如何在安卓Java中更改菜单标题颜色

huangapple go评论76阅读模式
英文:

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:
如何在安卓Java中更改菜单标题颜色

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

英文:

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:
如何在安卓Java中更改菜单标题颜色

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

答案1

得分: 2

标题组的颜色基于应用程序主题中的 android:textColorSecondary 项定义的颜色。

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

您可以使用以下代码覆盖 android:textColorSecondary 的颜色:

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

使用以下代码块:

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

如何在安卓Java中更改菜单标题颜色

英文:

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

&lt;style name=&quot;AppTheme&quot; parent=&quot;Theme.MaterialComponents.*&quot;&gt; 
    &lt;item name=&quot;android:textColorSecondary&quot;&gt;@color/....&lt;/item&gt;
&lt;/style&gt;

You can override the android:textColorSecondary color with:

&lt;com.google.android.material.navigation.NavigationView
    android:theme=&quot;@style/ThemeOverlay.titleColor&quot;
    ..&gt;

with:

  &lt;style name=&quot;ThemeOverlay.titleColor&quot; parent=&quot;&quot;&gt;
    &lt;item name=&quot;android:textColorSecondary&quot;&gt;@color/....&lt;/item&gt;
  &lt;/style&gt;

如何在安卓Java中更改菜单标题颜色

答案2

得分: 0

去你的 `strings.xml` 文件中写入以下内容:

    &lt;string name=&quot;namestring&quot;&gt;&lt;font fgcolor=&#39;#YOUR_COLOR&#39;&gt;others&lt;/font&gt;&lt;/string&gt;

现在前往你的 `menu.xml` 文件并将代码更改为以下内容:

    &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
    &lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

    &lt;group android:checkableBehavior=&quot;single&quot;&gt;
        &lt;item
            android:id=&quot;@+id/vibrationItem&quot;
            android:icon=&quot;@drawable/ic_baseline_vibration_24&quot;
            android:title=&quot;vibration&quot; /&gt;
        &lt;item
            android:id=&quot;@+id/Settings&quot;
            android:icon=&quot;@drawable/ic_baseline_settings_24&quot;
            android:title=&quot;Settings&quot; /&gt;
    &lt;/group&gt;
    &lt;item android:title=&quot;@string/namestring&quot;&gt;

        &lt;menu&gt;
            &lt;item android:title=&quot;Logout&quot;

            android:id=&quot;@+id/Logout&quot;
                android:icon=&quot;@drawable/logout&quot;/&gt;
        &lt;/menu&gt;

    &lt;/item&gt;
    &lt;/menu&gt;
英文:

go to your strings.xml file and write this:

&lt;string name=&quot;namestring&quot;&gt;&lt;font fgcolor=&#39;#YOUR_COLOR&#39;&gt;others&lt;/font&gt;&lt;/string&gt;

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

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;menu xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;

&lt;group android:checkableBehavior=&quot;single&quot;&gt;
    &lt;item
        android:id=&quot;@+id/vibrationItem&quot;
        android:icon=&quot;@drawable/ic_baseline_vibration_24&quot;
        android:title=&quot;vibration&quot; /&gt;
    &lt;item
        android:id=&quot;@+id/Settings&quot;
        android:icon=&quot;@drawable/ic_baseline_settings_24&quot;
        android:title=&quot;Settings&quot; /&gt;
&lt;/group&gt;
&lt;item android:title=&quot;@string/namestring&quot;&gt;

    &lt;menu&gt;
        &lt;item android:title=&quot;Logout&quot;

        android:id=&quot;@+id/Logout&quot;
            android:icon=&quot;@drawable/logout&quot;/&gt;
    &lt;/menu&gt;

&lt;/item&gt;
&lt;/menu&gt;

huangapple
  • 本文由 发表于 2020年8月24日 22:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63562890.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定