简体   繁体   中英

NavigationView menu item title color

Lets say I have a menu:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:title="@string/menu_title">
        <menu>
            <item
                android:id="@+id/nav_menu_item_1"
                android:icon="@drawable/nav_menu_item_1"
                android:title="@string/nav_menu_item_1" />
        </menu>
    </item>
</menu>

I can easily change color of "nav_menu_item_1" title like this:

<android.support.design.widget.NavigationView
    ...
    app:itemTextColor="@color/colorCustom"
    ... />

However this doesn't change color of

<item android:title="@string/menu_title">

How can I change color of first level menu item?

You need add android:textColorSecondary into your style.xml

 <item name="android:textColorSecondary">@color/colorPrimary</item>

it works for me

Ouptput

在此处输入图片说明

Set in Your theme... android:actionMenuTextColor

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:actionMenuTextColor">@color/colorAccent</item>
</style>

Or By Code

 MenuItem settingsMenuItem = menu.findItem(R.id.action_settings);
 SpannableString s = new SpannableString(settingsMenuItem.getTitle());
 s.setSpan(new ForegroundColorSpan(Color.BLACK), 0, s.length(), 0);
 settingsMenuItem.setTitle(s);

You can change it set theme to your NavigationView

Create in style.xml

<style name="NavigationView">
    <item name="android:background">?attr/clPrimary</item>
    <item name="android:textColorSecondary">@color/textSecond</item>
</style>

Set theme in layout

<com.google.android.material.navigation.NavigationView
    android:id="@+id/sheetAdd_nv_menu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/NavigationView"
    app:itemTextColor="?attr/clText"
    app:menu="@menu/menu_sheet_add" />

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