Android:按下菜单项时的背景色 [英] Android: background color when menu item is pressed

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

问题描述

我试图在按下菜单项时将其背景颜色更改为菜单项.更改背景颜色,但不更改按下时的颜色.

I'm trying to change the background color to the menu item when it is pressed. Change the background color but not the color when pressed.

怀抱:

  • 背景:深灰色
  • 按下的背景:橙色

已获取:

  • 背景:深灰色
  • 按下的背景:蓝色(默认为Android)

我能做什么?谢谢

Styles.xml

<style name="AppTheme2" parent="android:Theme.Holo">  
    <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
</style>

<style name="MyApp.PopupMenu" parent="android:Widget.Holo.ListPopupWindow">
    <item name="android:popupBackground">@drawable/menu_item_selector</item>
</style>

menu_item_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/menu_item_fondo_pressed" android:state_focused="true"/>
<item android:drawable="@drawable/menu_item_fondo_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/menu_item_fondo"/>

</selector>

推荐答案

答案不多,但是找到了解决问题的方法.

Little late answer, but found solution for the problem.

styles.xml 中,您拥有您的 AppTheme :

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:dropDownListViewStyle">@style/ListViewStyle</item>
    <item name="dropDownListViewStyle">@style/ListViewStyle</item>
    <item name="popupMenuStyle">@style/PopupMenu</item>
    <item name="textAppearanceLargePopupMenu">@style/PopupMenuTextAppearanceLarge</item>
    <item name="textAppearanceSmallPopupMenu">@style/PopupMenuTextAppearanceSmall</item>
    <item name="android:textAppearanceLargePopupMenu">@style/PopupMenuTextAppearanceLarge</item>
    <item name="android:textAppearanceSmallPopupMenu">@style/PopupMenuTextAppearanceSmall</item>
</style>

popupMenuStyle 用于popupMenu本身,在这种情况下,我们可以在 popupBackground 项中更改 unselected 背景,就像这样(但是您已经知道):

The popupMenuStyle is for popupMenu itself, in this case we can change there unselected background in popupBackground item, like this (but You already know that):

<style name="PopupMenu" parent="@android:style/Widget.PopupMenu">
    <item name="android:popupBackground">@drawable/selector_popup_menu_bg</item>
    <item name="android:textColor">@color/text_color_white</item>
    <item name="android:dropDownSelector">@drawable/selector_popup_menu_dropdown</item>
</style>

还有 textColor dropDownSelector 项,它们在我测试过的设备上没有任何作用,但是为了防万一,我还是在这里更改了它们,因为parent( Widget.PopupMenu )也使用它们.

There are also textColor and dropDownSelector items, which aren't doing anything on devices I have tested, but I change these here too just in case, because parent (Widget.PopupMenu) uses them too.

正确更改这些项目的方法是在 AppTheme 中对其进行更改,就像我上面在 AppTheme 代码中所示的那样.我不会显示textAppearances的代码,因为它不是主题.

The way to properly change these items is to change them in AppTheme like I've shown above in AppTheme code. I won't show code for textAppearances, because it is not the subject.

我将这些项目分别添加两次(使用和不使用"android:"前缀)的原因是使其在5.0和棒棒糖之前的设备上均可使用.唯一的例外是 popupMenuStyle ,其中只需要带有"android:"前缀的项目,如果您使用的是框架版本的Popup,但是如果使用的是support.v7版本,则需要android:popupMenuStyle(有关更多信息,请参见此StackOverflow答案).

The reason I've added each of these items twice (with and without "android:" prefix) is to make it work both on 5.0 and pre-lollipop devices. The only exception is popupMenuStyle with only needs item without "android:" prefix, if you’re using the framework version of Popup, but if you use the support.v7 version, then you need android:popupMenuStyle (see this StackOverflow answer for more info).

因此,要选择不同背景的项目,我们只需在 dropDownListViewStyle 中进行更改(我另外添加了 divider ):

So to have different background of item selected we simply change it in dropDownListViewStyle (I have additionaly added divider):

<style name="ListViewStyle" parent="@android:style/Widget.ListView">
    <item name="android:listSelector">@drawable/selector_popup_menu_dropdown</item>
    <item name="android:divider">@color/text_color_white</item>
    <item name="android:dividerHeight">1dp</item>
</style>

有趣的部分是 listSelector ,它是所选项目的背景.如果我们仅在其中添加@color,则所选项目不会正确无效(即使您将选择移出,它也会保持选中状态),要使其正确代替颜色,需要使用选择器:

The interesting part is listSelector, which is the background of selected item. If we would add there only @color then selected item wouldn't be properly invalidated (it will stay selected even if you move selection out of it), to make it properly instead of color, selector needs to be used:

selector_popup_menu_dropdown.xml

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

<item android:state_window_focused="false" android:drawable="@android:color/transparent" />

<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@color/gray_btn_bg_color" />
<item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@color/gray_btn_bg_color" />
<item android:state_focused="true"                                android:state_pressed="true" android:drawable="@color/gray_btn_bg_color" />
<item android:state_focused="false"                               android:state_pressed="true" android:drawable="@color/gray_btn_bg_color" />
<item android:state_focused="true"                                                             android:drawable="@color/gray_btn_bg_color" />

对于这种琐碎的事情,解释时间太长了,但是直到最后我们还是做了.耶!

A little too long explaination for such trival thing, but we made it till the end. Yaay!

这篇关于Android:按下菜单项时的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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