如何更改操作栏中下拉菜单的背景颜色

Posted

技术标签:

【中文标题】如何更改操作栏中下拉菜单的背景颜色【英文标题】:How to change the background color of dropdown menu in the actionbar 【发布时间】:2014-09-13 23:10:59 【问题描述】:

我正在开发一个 android 应用程序,但在设计我的操作栏时遇到了一些问题。我正在使用带有白色图标的深色操作栏。单击溢出图标时会弹出一个下拉菜单。一切都很好,但我需要带有红色文本的下拉菜单的浅色背景。我曾尝试在styles.xml 中编辑主题,但我只能在使用浅色主题时更改下拉菜单的背景颜色。

当我将Theme.Base.AppCompat.Light.DarkActionBar 更改为Theme.Base.AppCompat.Light 时,我可以获得浅色下拉菜单,但其他图标(如'搜索')变得非常浅且几乎不可见(参见第二张图片)。如何将下拉菜单上的文本颜色从黑色更改为红色,并使 '搜索' 图标可见。

这里是更多洞察的代码

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.Base.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:actionBarStyle">@style/MyActionBar</item>
    <item name="android:popupMenuStyle">@style/MyPopUpMenu</item>
</style>

<!-- ActionBar styles -->
<style name="MyActionBar"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:background">@color/gloryred</item>
</style>

<style name="MyPopUpMenu"
    parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
    <item name="android:popupMenuStyle">@color/white</item>
</style>

menu_actions.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:id="@+id/action_search"
    android:title="@string/action_search"
    android:icon="@drawable/ic_action_search"
    com.example.unitedstates.app:showAsAction="ifRoom"
    com.example.unitedstates.app:actionViewClass="android.widget.SearchView"/>

<item android:id="@+id/action_overflow"
    android:title="@string/action_overflow"
    android:icon="@drawable/ic_action_overflow"
    com.example.unitedstates.app:showAsAction="ifRoom">
    <menu>
        <item
            android:id="@+id/action_help"
            android:title="@string/action_help"
            android:icon="@drawable/ic_action_help"
            android:showAsAction="never"/>
        <item
            android:id="@+id/action_about"
            android:title="@string/action_about"
            android:icon="@drawable/ic_action_about"
            android:showAsAction="never"/>
        <item
            android:id="@+id/action_feedback"
            android:title="@string/action_feedback"
            android:icon="@drawable/ic_action_feedback"
            android:showAsAction="never"/>
    </menu>
</item>

【问题讨论】:

【参考方案1】:

您可以像这样在actionbar 中更改dropdown menu 的背景颜色和文本颜色。

在styles.xml中设置弹出菜单的样式。

 <style name="PopupMenuStyle" parent="Theme.AppCompat.Light">

         <item name="android:background">@color/YourColor</item>
         <item name="android:textColor">@Color/YourColor</item>

</style>

现在将此样式设置为您的toolbarpopupTheme

<android.support.v7.widget.Toolbar  
      xmlns:app="http://schemas.android.com/apk/res-auto"          
      xmlns:android="http://schemas.android.com/apk/res/android"

          // rest of your code
      app:popupTheme="@style/PopupMenuStyle" />

【讨论】:

【参考方案2】:

如果你想使用黑色背景,你可以去res &gt; drawable-hdpi &gt; menu_dropdown_panel_example.9.png 这是除了黑色线条之外的九个补丁图像,如果需要,可以将其颜色替换为黑色或红色

你必须这样做 res&gt;drawable-mdpi&gt;menu_dropdown_panel_example.9.png res&gt;drawable-xhdpi&gt;menu_dropdown_panel_example.9.png res&gt;drawable-xxhdpi&gt;menu_dropdown_panel_example.9.png

【讨论】:

【参考方案3】:

对于那些使用 androidx.appcompat 的 Material 主题的人来说,这是我的解决方案:

截至 appcompat:1.2.0 您需要创建一个 res/drawable/mtrl_popupmenu_background.xml 并将其设置为自定义颜色:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
  <solid android:color="?attr/colorPrimaryDark"/>

  <corners
      android:bottomLeftRadius="4dp"
      android:bottomRightRadius="4dp"
      android:topLeftRadius="4dp"
      android:topRightRadius="4dp"/>

  <padding
      android:bottom="8dp"
      android:top="8dp"/>

</shape>

然后创建一个专门的主题:

<style name="YouTheme.PopupMenuStyle">
    <item name="actionOverflowMenuStyle">@style/YouThemePopupMenu.Overflow</item>
</style>

<style name="YouThemePopupMenu" parent="@style/Widget.MaterialComponents.PopupMenu"/>

<style name="YouThemePopupMenu.Overflow" parent="@style/Widget.MaterialComponents.PopupMenu.Overflow">
    <item name="android:dropDownSelector">?attr/listChoiceBackgroundIndicator</item>
    <item name="android:popupBackground">@drawable/mtrl_popupmenu_background</item>
</style>

然后在你的工具栏中使用它:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_
    android:layout_
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:layout_scrollFlags="scroll|enterAlways"
    app:popupTheme="@style/YourTheme.PopupMenuStyle"/>

【讨论】:

以上是关于如何更改操作栏中下拉菜单的背景颜色的主要内容,如果未能解决你的问题,请参考以下文章

更改暴露下拉菜单的背景颜色

Android微调器下拉菜单背景颜色更改

Bootstrap:更改背景颜色下拉菜单

使用 Google 表格下拉菜单更改单元格背景颜色而不是文本

更改快餐栏中操作按钮的背景颜色

更改 actionBar 下拉背景颜色