简体   繁体   English

Android:按下菜单项时的背景色

[英]Android: background color when menu item is pressed

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. 按下时更改背景颜色,但不更改颜色。

WHISED: WHISED:

  • Background: dark gray 背景:深灰色
  • Background pressed: orange 背景按下:橙色

OBTAINED: 获得:

  • Background: dark gray 背景:深灰色
  • Background pressed: blue (default Android) 背景按下:蓝色(默认Android)

What I can do? 我可以做什么? Thanks 谢谢

在此输入图像描述

Styles.xml 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 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. 答案很少,但找到了问题的解决方案。

In styles.xml where You have your AppTheme : 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>

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): popupMenuStyle用于popupMenu本身,在这种情况下我们可以在popupBackground项中更改未选中的背景,就像这样(但你已经知道了):

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

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. 还有textColordropDownSelector项,它们在我测试的设备上没有做任何事情,但我也在这里更改这些以防万一,因为父( Widget.PopupMenu )也使用它们。

The way to properly change these items is to change them in AppTheme like I've shown above in AppTheme code. 正确更改这些项目的方法是在AppTheme中更改它们,就像我在AppTheme代码中所示。 I won't show code for textAppearances, because it is not the subject. 我不会显示textAppearances的代码,因为它不是主题。

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. 我之所以添加这些项目两次(有和没有“android:”前缀)的原因是为了让它在5.0和棒棒糖前设备上都能正常工作。 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 ). 唯一的例外是popupMenuStyle只需要没有“android:”前缀的项目,如果你使用的是Popup的框架版本,但是如果你使用的是support.v7版本,那么你需要android:popupMenuStyle请参阅此StackOverflow答案了解更多信息 )。

So to have different background of item selected we simply change it in dropDownListViewStyle (I have additionaly added divider ): 因此,要选择不同的项目背景,我们只需在dropDownListViewStyle中更改它(我添加了另外的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>

The interesting part is listSelector , which is the background of selected item. 有趣的部分是listSelector ,它是所选项目的背景。 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: 如果我们只在那里添加@color,那么选择的项目将无法正确无效(即使你将选择移出它也将保持选中状态),为了使其正确而不是颜色,需要使用选择器:

selector_popup_menu_dropdown.xml 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! Yaay!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM