我为背景、文本和图标创建了 3 个选择器。当我设置选择器时,我的导航视图如下所示:
问题是没有显示任何项目被选中,如果我按下一个项目,所有项目的背景颜色都会变为灰色。图标永远不会获得原色。
以下是 XML:
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
app:itemTextColor="@drawable/nav_item_text"
app:itemIconTint="@drawable/nav_item_icon_tint"
app:itemBackground="@drawable/nav_item_background"
app:headerLayout="@layout/drawer_student_data_header"
app:menu="@menu/drawer"
android:id="@+id/navigationView"/>
nav_item_icon_tint.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/gray_600"/>
<item android:state_selected="true" android:color="@color/primary" />
<item android:color="@color/gray_600" />
</selector>
nav_item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/black_87_percent"/>
<item android:state_selected="true" android:color="@color/primary" />
<item android:color="@color/black_87_percent" />
</selector>
nav_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/gray_400"/>
<item android:state_selected="true" android:drawable="@color/gray_200" />
<item android:drawable="@android:color/white" />
</selector>