【发布时间】:2020-03-02 12:56:55
【问题描述】:
我想在 NavigationDrawer 中设置所选项目的颜色和背景。 它适用于文本和图标,但不适用于所选项目背景。
ColorStateList textColors = new ColorStateList(
new int[][]{
new int[]{ -android.R.attr.state_checked }, // unchecked
new int[]{ android.R.attr.state_checked } // checked
},
new int[]{
getResources().getColor(R.color.menu_text_color),
getResources().getColor(R.color.md_black_1000)
}
);
navigationView.setItemTextColor(textColors);
navigationView.setItemIconTintList(textColors);
但是当我希望背景以同样的方式改变时,CHECKED状态不起作用,只有未选中状态......
ColorStateList backgroundColors = new ColorStateList(
new int[][]{
new int[]{ -android.R.attr.state_checked }, // unchecked
new int[]{ android.R.attr.state_checked } // checked
},
new int[]{
getResources().getColor(R.color.md_grey_400),
getResources().getColor(R.color.md_grey_700)
}
);
navigationView.setBackgroundTintList(backgroundColors);
navigationView.getMenu().findItem(/* menuitem id */).setChecked(true);
如何设置选中状态菜单项的背景,如文本和图标颜色?
【问题讨论】:
-
这设置了导航视图的整个背景...我只想设置选中的 MenuItems 背景,这就是为什么我想要这种选中和未选中的方法,就像文本和图标一样
标签: android navigation-drawer navigationview material-components-android android-navigationview