问题描述
我试图弄清楚如何更改实际附加到导航视图的子菜单项的颜色.以下代码实际上来自于android studio中可用的Navigation Drawer的默认模板.
I am trying to figure out how I can change color of sub-menu items which is actually attached to navigation view. Following codes are actually from default template of Navigation Drawer which is available in android studio.
activity_main_drawer.xml
activity_main_drawer.xml
<?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_camara"
android:icon="@android:drawable/ic_menu_camera"
android:title="Import"
/>
<item
android:id="@+id/nav_gallery"
android:icon="@android:drawable/ic_menu_gallery"
android:title="Gallery"/>
<item
android:id="@+id/nav_slideshow"
android:icon="@android:drawable/ic_menu_slideshow"
android:title="Slideshow"/>
<item
android:id="@+id/nav_manage"
android:icon="@android:drawable/ic_menu_manage"
android:title="Tools"/>
</group>
<item android:title="Communicate">
<menu>
<item
android:id="@+id/nav_share"
android:icon="@android:drawable/ic_menu_share"
android:title="Share"/>
<item
android:id="@+id/nav_send"
android:icon="@android:drawable/ic_menu_send"
android:title="Send"/>
</menu>
</item>
</menu>
activity_main.xml
activity_main.xml
<include
layout="@layout/app_bar_main"
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="start"
android:fitsSystemWindows="true"
android:background="#512DA8"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
app:itemTextColor="@drawable/nav_menu_item_color"
app:itemIconTint="@drawable/nav_menu_item_color"/>
</android.support.v4.widget.DrawerLayout>
,我有一个可绘制的文件,用于单击和上面实际使用的正常颜色.nav_menu_item_color.xml
and I have a drawable file for click and normal color which is actualy used above.nav_menu_item_color.xml
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<selector>
<item android:color="@color/navTextHover" android:state_checked="true" />
<item android:color="@color/navTextNormal" />
</selector>
因此,您可以看到如下所示的结果图像:-
So, you can see result image as I attached below :-
颜色不能用于子菜单,因此,我想做的是..我想更改菜单项"communication"和子菜单项"send"和"share"的颜色,因为它适用于根菜单项目
Colors not working for sub menu, so, what I want to do is .. I want to change color of menu item "communicate" and sub-menu item "send" and "share" as it is working for root menu items
推荐答案
您的子菜单需要包含在菜单&组标签如下.这将允许您一次选择任何一个菜单项.您可以通过将其设置为在NavigationView.OnNavigationItemSelectedListener中选中的项目来选择它们.
Your submenu needs to be wrapped in a menu & group tag like below. This will allow you to select one of any of the menu items at a time. You can select them by setting the item as checked in your NavigationView.OnNavigationItemSelectedListener.
<?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/nav1"
android:checked="true"
android:icon="@drawable/myd1"
android:title="Nav 1"
/>
</group>
<item android:title="@string/nav_item_subheading_app">
<menu>
<group android:checkableBehavior="single">
<item
android:id="@+id/nav1"
android:icon="@drawable/myd1"
android:title="Nav 1"
/>
</group>
</menu>
</item>
</menu>
这篇关于如何更改导航视图附带的子菜单的文本和图标的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!