Android: background color when clicking a menu item

I am trying to change the background color to a menu item when clicked. Change the background color, but not the color when clicked.

WHISED:

  • Background: Dark Gray
  • Background Printing: Orange

RECEIVED:

  • Background: Dark Gray
  • Background Printing: Blue (Android by default)

What can I do? Thanks

enter image description here

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>
+4
source share
1 answer

The answer is a bit late, but found a solution to the problem.

In styles.xml, where you have the 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 for popupMenu itself, in this case we can change the unselected background to popupBackground, like this (but you already know this):

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

, AppTheme, AppTheme. textAppearances, .

, ( "android:" ), 5.0, - . popupMenuStyle "android:", Popup, support.v7, android:popupMenuStyle (fooobar.com/questions/1561635/...).

, dropDownListViewStyle ( ):

<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, ( , ), , :

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

, . Yaay!

+1

All Articles