更改弹出菜单的背景颜色 [英] Changing the background color of a Popup menu

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

问题描述

在我的Android应用中,我使用的是弹出菜单,但背景为绿色.
我希望它是白色的.

如果我在Android 6.0设备上运行背景为白色,但在Jellybean设备上显示为绿色.

我不使用操作栏.
关于此问题的帖子很多,我可能已经尝试了大多数,但找不到解决方案.

距离我最近的是白色背景,但边框消失了.
或者我得到了白色背景,但是当按下某个项目时,所选内容将显示灰色背景,但文字背景显示为白色.
我正在使用AppCompat.

以下是我尝试过的一些事情:

 <样式名称="AppTheme" parent ="Theme.AppCompat.Light.NoActionBar">< item name ="windowActionBar"> false</item>< item name ="colorControlActivated">#d06655</item>< item name ="colorAccent">#ffe6e2</item>< item name ="android:colorActivatedHighlight">#ffe6e2</item>< item name ="android:windowBackground"> @ color/white</item><!-< item name ="android:popupMenuStyle"> @ style/PopupMenu</item>-><!-< item name ="android:itemBackground"> @ color/white</item>->< item name ="popupMenuStyle"> @ style/myPopupMenuStyle</item>< item name ="android:popupMenuStyle"> @ style/myPopupMenuStyle</item></style><!-< style name ="PopupMenu" parent ="Widget.AppCompat.PopupMenu">-><!-< style name ="PopupMenu" parent ="@ style/Widget.AppCompat.PopupMenu">->< style name ="myPopupMenuStyle" parent ="@ style/Widget.AppCompat.Light.PopupMenu">< item name ="android:popupBackground"> @android:color/white</item></style> 

解决方案

您可以使用

所以我要做的就是-在生成器中指定适当的颜色,重新获得9patched背景(menu_dropdown_panel_background.9.png)并将其设置为背景:

 <样式名称="AppTheme" parent ="Theme.AppCompat.Light.DarkActionBar">< item name ="popupMenuStyle"> @ style/PopupMenu.Example</item></style>< style name ="PopupMenu.Example" parent ="@ style/Widget.AppCompat.Light.PopupMenu">< item name ="android:popupBackground"> @ drawable/menu_dropdown_panel_background</item></style> 

就是这样!

In my Android app, I am using a popup menu but its background is green.
I want it to be white.

The background is white if I run it on my Android 6.0 device, but shows up green on my Jellybean device.

I'm not using the Actionbar.
There are many posts about this issue and I've tried probably most of them but can't find the solution.

The closest I got was a white background but then the border disappears.
Or I get a white background but when pressing on an item, the selection shows a grey background except for the text's background which shows white.
I'm using AppCompat.

Here are some of the things I've tried:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="colorControlActivated">#d06655</item>
    <item name="colorAccent">#ffe6e2</item>
    <item name="android:colorActivatedHighlight">#ffe6e2</item>
    <item name="android:windowBackground">@color/white</item>
    <!--<item name="android:popupMenuStyle">@style/PopupMenu</item>-->
    <!--<item name="android:itemBackground">@color/white</item>-->
    <item name="popupMenuStyle">@style/myPopupMenuStyle</item>
    <item name="android:popupMenuStyle">@style/myPopupMenuStyle</item>
</style>

<!--<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">-->
<!--<style name="PopupMenu" parent="@style/Widget.AppCompat.PopupMenu">-->
<style name="myPopupMenuStyle" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

解决方案

You can brash the popup menu by using Android Action Bar Style Generator(or create a 9patch background by your own).

So all I did was - specify proper colour in the generator, get 9patched background(menu_dropdown_panel_background.9.png) back and set it as a background:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="popupMenuStyle">@style/PopupMenu.Example</item>
</style>

<style name="PopupMenu.Example" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_background</item>
</style>

That's it!

这篇关于更改弹出菜单的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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