更改默认导航菜单的颜色

时间:2016-03-02 05:13:29

标签: android android-navigationview

我在很多地方都看到了这个问题,但他们在导航菜单中使用了自定义列表视图。

我的导航视图代码如下:

<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);

如何更改菜单中的所选项目?

2 个答案:

答案 0 :(得分:1)

尝试更改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>

答案 1 :(得分:0)

最后,在尝试了一切后,我得到了答案 您必须将颜色文件中的colorAccent更改为您想要的颜色:

  <color name="colorAccent">whichever color required</color>

此解决方案适合我