如何在操作栏中更改下拉菜单的背景颜色 [英] How to change the background color of dropdown menu in the actionbar

查看:201
本文介绍了如何在操作栏中更改下拉菜单的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm working on an android app and I'm facing some problems in designing my actionbar. I'm using the dark actionbar with white icons on it. I have a dropdown menu popped when I click the overflow icon. Everything is good but I need light background for the dropdown menu with red colored text. I have tried editing the theme in styles.xml but I can only change the background color of dropdown menu when I'm using the light colored theme.

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

I can get the light colored dropdown menu when I change the Theme.Base.AppCompat.Light.DarkActionBar to Theme.Base.AppCompat.Light but other icons like 'Search' turn very light and hardly visible (see 2nd picture). How can I change the text color on the drop down menu from black to red and make the 'Search' icon visible.

这里是获得更多洞察力的代码

Here is the code for more insight

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>

推荐答案

您可以像这样在 actionbar 中更改下拉菜单的背景颜色和文本颜色.

You can change the background color and text color of the dropdown menu in the actionbar like this.

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

Set the style for the popup menu in styles.xml.

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

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

</style>

现在将这种样式设置为工具栏 popupTheme .

Now set this style as the popupTheme for your toolbar.

<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" />

这篇关于如何在操作栏中更改下拉菜单的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆