导航视图的选择行为不正确 [英] Navigation View's selectors behave incorrectly
问题描述
我创建3选择,背景,文本和图标。
当我设置的选择,我的导航视图是这样的:
问题是,没有项目显示选择,如果我preSS一个项目,所有项目的背景颜色变为灰色。图标永远不会原色。
在这里是XML的:
< android.support.design.widget.NavigationView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =match_parent
机器人:layout_gravity =开始
机器人:背景=@机器人:彩色/白
应用:itemTextColor =@绘制/ nav_item_text
应用:itemIconTint =@绘制/ nav_item_icon_tint
应用:itemBackground =@绘制/ nav_item_background
应用:headerLayout =@布局/ drawer_student_data_header
应用:菜单=@菜单/抽屉
机器人:ID =@ + ID / navigationView/>
nav_item_icon_tint.xml
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真正的机器人:颜色=@色/ gray_600/>
<项目的android:state_selected =真正的机器人:颜色=@色/主/>
<项目的android:颜色=@色/ gray_600/>
< /选择>
nav_item_text.xml
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真正的机器人:颜色=@色/ black_87_percent/>
<项目的android:state_selected =真正的机器人:颜色=@色/主/>
<项目的android:颜色=@色/ black_87_percent/>
< /选择>
nav_item_background.xml
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目的android:STATE_ pressed =真正的机器人:可绘制=@色/ gray_400/>
<项目的android:state_selected =真正的机器人:可绘制=@色/ gray_200/>
<项目机器人:可绘制=@机器人:彩色/白/>
< /选择>
您可以使用 setItemTextColor
和 setItemIconTintList
的设置文本颜色的色调和图标色彩按NavigationDrawer项目,
要使用此方法添加此code结束onCreateView的:
INT [] [] =国新INT [] [] {
新的INT [] { - android.R.attr.state_checked} //未选中状态
新的INT [] {} android.R.attr.state_checked,//选中状态
};INT [] =颜色INT新[] {
ContextCompat.getColor(这一点,R.color.colorPrimary)
ContextCompat.getColor(这一点,R.color.colorPrimaryDark)
};ColorStateList colorStateList =新ColorStateList(州,颜色);
navigationView.setItemTextColor(colorStateList);
navigationView.setItemIconTintList(colorStateList);
这code设置颜色为未选中状态,选中状态。您可以设置颜色到其他国家。
颜色阵列和阵列的状态应具有相当于会员数。您可以选择任何你想要的颜色。
您可以使用Color类或喜欢使用样本资源。在这个环节上,你可以找到可用状态 developer.android 列表。
I'm following this color scheme of Navigation View.
I created 3 selectors, for background, text and icon. When I set the selectors, my navigation view looked like this:
The problem is that no item is shown selected and if I press one item, all items' background color changes to gray. Icon never gets the primary color.
Here is are the XMLs:
<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>
You can use setItemTextColor
and setItemIconTintList
for set text color tint and icon color tint to item of NavigationDrawer,
To use this methods add this code to end of onCreateView:
int[][] states = new int[][]{
new int[]{-android.R.attr.state_checked},// unchecked state
new int[]{android.R.attr.state_checked}, // checked state
};
int[] colors = new int[]{
ContextCompat.getColor(this, R.color.colorPrimary),
ContextCompat.getColor(this, R.color.colorPrimaryDark)
};
ColorStateList colorStateList = new ColorStateList(states, colors);
navigationView.setItemTextColor(colorStateList);
navigationView.setItemIconTintList(colorStateList);
This code set color to unchecked state and checked state. You can set color to other states.
Color array and state array should be have equals member count. You can select color whatever you want. You can use Color class or like the sample use resource. In this link you can find list of available states developer.android.
这篇关于导航视图的选择行为不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!