样式appcompat-v7工具栏菜单背景

vin*_*mr3 6 android android-layout android-toolbar

我正在尝试将appcompat-v7工具栏设置为我的溢出菜单的不同背景颜色.我尝试使用我的应用程序的主题和我的工具栏的样式,但我无法实现它.

这是我的工具栏:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"                                                                                                                              
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:layout_width="match_parent"
    app:theme="@style/AppToolbarTheme"
    android:layout_height="wrap_content">
Run Code Online (Sandbox Code Playgroud)

这是我创建的风格:

    <style name="AppToolbarTheme" parent="Theme.AppCompat.NoActionBar">
       <item name="android:textColorPrimary">@color/white</item>
       <item name="android:textColorSecondary">@color/cian</item>
    </style>
Run Code Online (Sandbox Code Playgroud)

我的主题是扩展Theme.AppCompat.Light.

有谁知道我该怎么做?如果不可能使用样式有没有其他方法来实现它?

Eug*_*nec 35

将其添加到工具栏元素

app:popupTheme="@style/ThemeOverlay.YourPopup"
Run Code Online (Sandbox Code Playgroud)

然后在您的styles.xml定义弹出菜单样式

<style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/mtrl_white_100</item>
    <item name="android:textColor">@color/mtrl_light_blue_900</item>
</style>
Run Code Online (Sandbox Code Playgroud)

<style name="ThemeOverlay.YourPopup" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/mtrl_white_100</item>
    <item name="android:textColorPrimary">@color/mtrl_light_blue_900</item>
</style>
Run Code Online (Sandbox Code Playgroud)

请注意,您需要使用android:colorBackground而且从不使用android:background.后者将应用于没有背景的所有内容(此处为菜单本身和每个菜单项),前者仅适用于弹出菜单.

更新:同样适用于textColorPrimarytextColor.

  • 弹出菜单项定义android:textColor="?android:textColorPrimary".
  • android:textColorPrimary是一个主题属性,它是在主题上定义的.
  • android:textColor是一个样式属性,它在小部件上定义.
  • 如果我们android:textColor在主题中定义,它将应用于每个未定义自己的小部件android:textColor.