如何使用Java以编程方式更改导航抽屉上所选菜单项的文本和图标颜色 [英] How to change the text and icon color of selected menu item on Navigation Drawer programmatically using java
问题描述
我是Android开发的初学者.使用android的默认导航抽屉活动,我正在开发一个应用程序.此应用程序的要求之一是在运行时随机更改布局的背景颜色(包括导航抽屉标题颜色).
I am a beginner in Android Development. Using android's default Navigation drawer activity I am developing an application. One of the requirements of this app is to change the background colors of layouts (including navigation drawer header color) randomly at run time.
现在一切正常,除了导航抽屉上所选菜单项的颜色仍然为蓝色.像这样:
Now everything is going fine except the color of the selected menu item on navigation drawer is still blue. like this :
现在我想要的是其他布局的背景颜色是粉红色,因此导航栏上的所选菜单项也应该是粉红色的(我的意思是文本颜色和图标应该是粉红色的),如下所示:
有人可以告诉我如何以编程方式实现它,因为我必须在运行时随机更改选定的文本和图标颜色.
这是菜单xml文件供参考:
Here's the menu xml file for reference:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="@+id/nav_quiz_all"
android:icon="@drawable/ic_public_black_24dp"
android:checked="true"
android:title="All Countries Quiz"/>
<item
android:id="@+id/nav_quiz_bookmarked"
android:icon="@drawable/ic_favorite_black_24dp"
android:title="Favorite Quiz"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_rate"
android:icon="@drawable/ic_star_black_24dp"
android:title="Rate this app"/>
<item
android:id="@+id/nav_share"
android:icon="@drawable/ic_share_black_24dp"
android:title="Share"/>
<item
android:id="@+id/nav_feedback"
android:icon="@drawable/ic_feedback_black_24dp"
android:title="Feedback"/>
<item
android:id="@+id/nav_about"
android:icon="@drawable/ic_info_black_24dp"
android:title="About"/>
</menu>
</item>
<item
android:id="@+id/nav_settings"
android:icon="@drawable/ic_settings_black_24dp"
android:title="Settings"/>
</menu>
推荐答案
首先感谢大家对您的解决方案的答复:)从上面的答案中学习并对ColorStateList
进行了一些研究,最终我设法创建了
Firstly thank you all for responding back with your solutions :) Learning from the answers above and doing some research on ColorStateList
I finally managed to create a method
which sets the color of the checked item on the navigation drawer to match the color of my app theme color which is generated randomly at runtime.
以下是方法:
public void setNavMenuItemThemeColors(int color){
//Setting default colors for menu item Text and Icon
int navDefaultTextColor = Color.parseColor("#202020");
int navDefaultIconColor = Color.parseColor("#737373");
//Defining ColorStateList for menu item Text
ColorStateList navMenuTextList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor,
navDefaultTextColor
}
);
//Defining ColorStateList for menu item Icon
ColorStateList navMenuIconList = new ColorStateList(
new int[][]{
new int[]{android.R.attr.state_checked},
new int[]{android.R.attr.state_enabled},
new int[]{android.R.attr.state_pressed},
new int[]{android.R.attr.state_focused},
new int[]{android.R.attr.state_pressed}
},
new int[] {
color,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor,
navDefaultIconColor
}
);
mNavView.setItemTextColor(navMenuTextList);
mNavView.setItemIconTintList(navMenuIconList);
}
您可以使用所需的任何int color
调用此方法:)
you can call this method with any int color
you want :)
这篇关于如何使用Java以编程方式更改导航抽屉上所选菜单项的文本和图标颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!