简体   繁体   中英

How to change item text color from a NavigationDrawer menu using styles.xml?

I have a Styles.xml file describing the colors of my NavigationDrawer:

  <style name="NavigationDrawerStyle">
        <item name="android:textSize">16sp</item><!-- text size in menu-->
        <item name="android:textColor">#880ACE0A</item>
    </style>

I know that you should change this property of NavigationDrawer for the text colour: "app:itemTextColor", so I tried adding it to my styles.xml file:

  <style name="NavigationDrawerStyle">
        <item name="android:textSize">16sp</item><!-- text size in menu-->
        <item name="android:textColor">#880ACE0A</item>
        <item name="app:itemTextColor">#880ACE0A</item>
    </style>

After adding it I have this compilation error on the file styles.xml:

Error:(12, 5) No resource found that matches the given name: attr 'app:itemTextColor'.

How to set the colour of the text on the items of a NavigationDrawer menu?

Edit

This is my NavigationDrawer:

<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    android:theme="@style/NavigationDrawerStyle"
    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/drawer_menu"/>

And this is my drawer_menu.xml file:

<?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_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_manage"
            android:icon="@drawable/ic_menu_manage"
            android:title="Tools" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

Edit 2

I believe this is not a duplicate of this question because it is mandatory for this case to specify the color in styles.xml file.

@NullPointerException

in the styles.xml file just use the item name without the custom tag. For example:

<style name="NavigationDrawerStyle">
        <item name="android:textSize">16sp</item><!-- text size in menu-->
        <item name="android:textColor">#880ACE0A</item>
        <item name="itemTextColor">#880ACE0A</item>
</style>

Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM