1

Ich benutze die Android-Support-Bibliothek, um eine Navigationsleiste zu erstellen.Navigationsstatus in Aktionslayout propagieren

<android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     android:background="@color/white" 
     app:headerLayout="@layout/nav_header_landing" 
     app:menu="@menu/activity_dashboard_drawer" 
     app:theme="@style/MyActionButtonStyle"/> 

ich benutzerdefinierte Menüpunkte erschaffe die actionLayout

<group android:checkableBehavior="single" 
     android:id="@+id/gp_dashboard"> 
     <item 
      android:id="@+id/nav_dashboard" 
      app:actionLayout="@layout/layout_dashboard" 
      android:icon="@mipmap/ic_dashboard" 
      android:title=""/> 
    </group> 

Verwendung Dies ist das Layout "layout_dashboard"

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:duplicateParentState="true"> 

    <helpers.customViews.RobotoTextView 
     android:id="@+id/dash_title" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="DASHBOARD" 
     app:typeface="robotoMedium" 
     android:textColor="@color/nav_header_state_list" 
     android:textSize="14sp"/> 

    <helpers.customViews.RobotoTextView 
     android:id="@+id/dash_subtitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/dash_title" 
     android:text="@string/dashboard_txt" 
     app:typeface="robotoRegular" 
     android:textColor="@color/nav_sub_header_state_list" 
     android:textSize="12sp"/> 

</RelativeLayout> 

Jetzt möchte ich die Textfarbe dieses Layout ändern, wenn der Menüpunkt ist ausgewählt. Um dies zu tun bin ich mit der @color/nav_header_state_list

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/greeny_blue" android:state_checked="true"/> 
    <item android:color="@color/greeny_blue" android:state_pressed="true" /> 
    <item android:color="@color/black"/> 
</selector> 

Aber der Farbe bleibt gleich, auch wenn der Menüpunkt ausgewählt wird. Die pressed state funktioniert, aber die checked state nicht. Bitte helfen Sie mir, das Problem zu lösen.

Antwort

1
  • ersetzen state_checked mit state_selected im nav_header_state_list.xml

Ihre nav_header_state_list.xml sollte wie folgt aussehen:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/greeny_blue" android:state_pressed="true" /> 
    <item android:color="@color/greeny_blue" android:state_selected="true" /> 
    <item android:color="@color/black" /> 
</selector> 
  • Deklarieren MenuItem Objekt in der M ainActivity.java
  • Fügen Sie die folgende Logik zum onNavigationItemSelected

Ihre MainActivity.java sollte wie folgt aussehen:

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    MenuItem previousItem; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // ... some code 
    } 

    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 
     // ... some code 

     if (previousItem != null) { 
      View previousActionView = previousItem.getActionView(); 
      previousActionView.findViewById(R.id.dash_title).setSelected(false); 
      previousActionView.findViewById(R.id.dash_subtitle).setSelected(false); 
     } 
     View actionView = item.getActionView(); 
     actionView.findViewById(R.id.dash_title).setSelected(true); 
     actionView.findViewById(R.id.dash_subtitle).setSelected(true); 
     previousItem = item; 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 
} 
+0

Das ist mein Problem löst. Vielen Dank – amarok

Verwandte Themen