简体   繁体   English

如何使用styles.xml 从NavigationDrawer 菜单更改项目文本颜色?

[英]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:我有一个 Styles.xml 文件描述了我的 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:我知道您应该为文本颜色更改 NavigationDrawer 的此属性:“app:itemTextColor”,因此我尝试将其添加到我的 styles.xml 文件中:

  <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:添加它后,我在文件 style.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?如何设置 NavigationDrawer 菜单项上的文本颜色?

Edit编辑

This is my NavigationDrawer:这是我的 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:这是我的 drawer_menu.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_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编辑 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.我相信这不是这个问题的重复,因为在这种情况下必须在 styles.xml 文件中指定颜色。

@NullPointerException @空指针异常

in the styles.xml file just use the item name without the custom tag.在styles.xml 文件中只使用没有自定义标签的项目名称。 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干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过styles.xml改变MaterialButton的背景颜色 - How to change background color of MaterialButton through styles.xml 如何将默认工具栏标题文本从@string/app_name 更改为“”,但仅更改 styles.xml? - How to change default Toolbar title text from @string/app_name to “”, but only changing styles.xml? 如何在NavigationDrawer材质设计中更改每个菜单项的背景颜色? - How to change background color for each menu item in NavigationDrawer material design? 通过styles.xml更改EditText光标的颜色 - Change EditText Cursor color through styles.xml 从 styles.xml 切换应用程序主题时,是否可以更改项目的可见性? - Is there a way to change visibility of an item when switching app theme from styles.xml? Android样式:如何更改styles.xml中的颜色 - Android styles: how to change colors in styles.xml 无法使用styles.xml设置按钮背景色 - Cannot set button background color using styles.xml 如何将样式从styles.xml应用到跨度? - How to apply styles from styles.xml to span? 安卓 | 使用styles.xml 更改TextInputLayout 的主题 - Android | Change theme of TextInputLayout using styles.xml 如何在运行时使用styles.xml更改活动的主题 - How do I change the Activity's theme using styles.xml at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM