简体   繁体   中英

how to change menu title color android java

I need to change the title color of my menu on my drawer navigation activity.

This is part of my activity_main.xml :

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/naView"
    android:fitsSystemWindows="false"
    android:background="@drawable/navbackground"
    android:layout_gravity="start"
    app:itemIconTint="@color/grayWhite"
    app:headerLayout="@layout/navigationheader"
    app:menu="@menu/menu">

And this is my activity_main_drawer.xml : (this file is inside the menu folder)

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <group android:checkableBehavior="single">
        <item
            android:id="@+id/vibrationItem"
            android:icon="@drawable/ic_baseline_vibration_24"
            android:title="vibration" />
        <item
            android:id="@+id/Settings"
            android:icon="@drawable/ic_baseline_settings_24"
            android:title="Settings" />
    </group>
    <item android:title="others">
        <menu>
            <item android:title="Logout"
            android:id="@+id/Logout"
                android:icon="@drawable/logout"/>
        </menu>
    </item>
</menu>

This is what I get from my code, and how you see the "other" is displayed with a different color:截屏

I saw many solutions on StackOverflow but I have not found a good solution for my problem.

The color of the title group is based on color defined by the android:textColorSecondary item in your app theme.

<style name="AppTheme" parent="Theme.MaterialComponents.*"> 
    <item name="android:textColorSecondary">@color/....</item>
</style>

You can override the android:textColorSecondary color with:

<com.google.android.material.navigation.NavigationView
    android:theme="@style/ThemeOverlay.titleColor"
    ..>

with:

  <style name="ThemeOverlay.titleColor" parent="">
    <item name="android:textColorSecondary">@color/....</item>
  </style>

在此处输入图片说明

go to your strings.xml file and write this:

<string name="namestring"><font fgcolor='#YOUR_COLOR'>others</font></string>

now go to your menu.xml file and change your code to this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<group android:checkableBehavior="single">
    <item
        android:id="@+id/vibrationItem"
        android:icon="@drawable/ic_baseline_vibration_24"
        android:title="vibration" />
    <item
        android:id="@+id/Settings"
        android:icon="@drawable/ic_baseline_settings_24"
        android:title="Settings" />
</group>
<item android:title="@string/namestring">

    <menu>
        <item android:title="Logout"

        android:id="@+id/Logout"
            android:icon="@drawable/logout"/>
    </menu>

</item>
</menu>

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