简体   繁体   中英

How to change the color of the menu text

The manifest specifies a theme for the entire application

android:theme="@style/Theme.MyApplication"

In themes.xml, this theme inherits:

<style name="Theme.MyApplication" parent="Theme.MaterialComponents.Light.NoActionBar"></style>

Instead of ActionBar, I put the ToolBar in the activity in which I changed the theme:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:theme="@style/ThemeForMyMenu"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#47A0FF"/>

In themes.xml, the ThemeForMyMenu is described as follows:

<style name="ThemeForMyMenu" parent="@style/ThemeOverlay.MaterialComponents.Dark.ActionBar">
    <item name="android:actionMenuTextColor">#000000</item>
</style>

As a result, it turns out that I use a light theme for the entire application, and a dark one for the ToolBar in which I change the actionMenuTextColor to black. I create a menu file in which I create two items. one item is displayed on the ToolBar, and the other remains in the menu

<item android:title="О программе"
    app:showAsAction="always"
    android:id="@+id/about"
    />
<item android:title="Выход"
    android:id="@+id/exit"
    app:showAsAction="never"/>

The item that was displayed on the ToolBar turned black (# 000000), and the item that remained in the menu has white color and merges with the background of the menu (it is not visible). I need to change the menu text color and menu background color. I couldn't find a way to do this. Tell me how to change the text color (inside the menu) and the background color of the menu

You can add the app:popupTheme attribute in your Toolbar .

<androidx.appcompat.widget.Toolbar
    app:popupTheme="@style/AppTheme.PopupOverlay" 
    ../>

with:

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.MaterialComponents.Toolbar.Primary" >
    <item name="android:textColor">@color/...</item>   <!-- text color -->
    <item name="colorSurface">@color/...</item>  <!-- background -->
</style>

在此处输入图像描述

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