更改默认导航菜单的颜色
Change color of default navigation menu
我在很多地方都看到过这个问题,但他们在导航菜单中使用了自定义列表视图。
我的导航视图代码是这样的:
<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"
app:headerLayout="@layout/nav_header_insta_drawer"
app:menu="@menu/activity_insta_drawer_drawer" />
我的@menu/activity_insta_drawer_drawer 是这样的:
<?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_applnform"
android:icon="@drawable/application_form"
android:title="Application Form"
android:checkable="true"
/>
<item
android:id="@+id/nav_penddocs"
android:icon="@drawable/pending_doc"
android:title="Pending Documents"
/>
<item
android:id="@+id/nav_viewdocs"
android:icon="@drawable/view_doc"
android:title="View Documents" />
<item
android:id="@+id/nav_payfee"
android:icon="@drawable/pay_free"
android:title="Pay Fee" />
<item
android:id="@+id/nav_refereedetail"
android:icon="@drawable/refree_detail"
android:title="Referee Details" />
<item
android:id="@+id/nav_informcorner"
android:icon="@drawable/information"
android:title="Information Corner" />
<item
android:id="@+id/nav_changepass"
android:icon="@drawable/change_pass"
android:title="Change Password" />
<item
android:id="@+id/nav_postquery"
android:icon="@drawable/ic_menu_send"
android:title="Post Query" />
</group>
</menu>
导航视图代码:
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
如何更改菜单的选定项?
终于在尝试了一切之后我得到了答案
您必须将颜色文件中的 colorAccent 更改为您想要的颜色:
<color name="colorAccent">whichever color required</color>
这个解决方案对我有用
尝试像下面这样更改您的 NavigationView
,添加 app:itemTextColor="@drawable/nav_draw_selector"
以更改导航菜单文本颜色:
<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"
app:itemTextColor="@drawable/nav_draw_selector"
app:headerLayout="@layout/nav_header_insta_drawer"
app:menu="@menu/activity_insta_drawer_drawer" />
nav_header_insta_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#4281ca" android:state_checked="true" /> <!-- This is the default text color -->
<item android:color="#DEDEDE" />
</selector>
我在很多地方都看到过这个问题,但他们在导航菜单中使用了自定义列表视图。
我的导航视图代码是这样的:
<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"
app:headerLayout="@layout/nav_header_insta_drawer"
app:menu="@menu/activity_insta_drawer_drawer" />
我的@menu/activity_insta_drawer_drawer 是这样的:
<?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_applnform"
android:icon="@drawable/application_form"
android:title="Application Form"
android:checkable="true"
/>
<item
android:id="@+id/nav_penddocs"
android:icon="@drawable/pending_doc"
android:title="Pending Documents"
/>
<item
android:id="@+id/nav_viewdocs"
android:icon="@drawable/view_doc"
android:title="View Documents" />
<item
android:id="@+id/nav_payfee"
android:icon="@drawable/pay_free"
android:title="Pay Fee" />
<item
android:id="@+id/nav_refereedetail"
android:icon="@drawable/refree_detail"
android:title="Referee Details" />
<item
android:id="@+id/nav_informcorner"
android:icon="@drawable/information"
android:title="Information Corner" />
<item
android:id="@+id/nav_changepass"
android:icon="@drawable/change_pass"
android:title="Change Password" />
<item
android:id="@+id/nav_postquery"
android:icon="@drawable/ic_menu_send"
android:title="Post Query" />
</group>
</menu>
导航视图代码:
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
如何更改菜单的选定项?
终于在尝试了一切之后我得到了答案 您必须将颜色文件中的 colorAccent 更改为您想要的颜色:
<color name="colorAccent">whichever color required</color>
这个解决方案对我有用
尝试像下面这样更改您的 NavigationView
,添加 app:itemTextColor="@drawable/nav_draw_selector"
以更改导航菜单文本颜色:
<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"
app:itemTextColor="@drawable/nav_draw_selector"
app:headerLayout="@layout/nav_header_insta_drawer"
app:menu="@menu/activity_insta_drawer_drawer" />
nav_header_insta_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#4281ca" android:state_checked="true" /> <!-- This is the default text color -->
<item android:color="#DEDEDE" />
</selector>