我是StackOverFlow的新手,这是我的第一个问题。如果我犯了任何错误,请原谅我。
我在NavigationView
内DrawerLayout
看起来像这样
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:fitsSystemWindows="true"
android:background="@android:color/white"
app:itemBackground="@drawable/nav_item_selector"
app:itemIconTint="@color/nav_icon_color"
app:itemTextColor="@color/nav_text_color"
app:headerLayout="@layout/nav_header_home_screen"
app:menu="@menu/activity_home_screen_drawer">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
下面是我的nav_item_selector,nav_icon_color,nav_text_color类
nav_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/fb_color" android:state_pressed="true"/>
<item android:drawable="@android:color/white" android:state_pressed="false"/>
</selector>
nav_icon_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_pressed="true" />
<item android:color="#2e9f68" android:state_pressed="false" />
</selector>
nav_text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#b5edd2" android:state_pressed="true" />
<item android:color="#222222" android:state_pressed="false" />
</selector>
这适用于API&gt; 15但是在API 15上只有itemBackground
正在运行,但itemIconTint
和itemTextColor
statePressed
无效。不知道为什么。
我还尝试在我的Activity
MainActivity
navigationView = (NavigationView) findViewById(R.id.nav_view);
int[][] states = new int[][] {
new int[] { android.R.attr.state_enabled}, // enabled
new int[] {-android.R.attr.state_enabled}, // disabled
new int[] {-android.R.attr.state_checked}, // unchecked
new int[] { android.R.attr.state_pressed} // pressed
};
int[] colors = new int[] {
Color.WHITE,
Color.RED,
Color.GREEN,
Color.BLUE
};
navigationView.setItemTextColor(new ColorStateList(states,colors));
下面是API 15和API 19的屏幕截图。我已经在API 16中对此进行了测试,并且工作正常。