如何在Android弹出菜单项上更改按下的颜色?

时间:2017-07-18 14:10:08

标签: android

我有一个像这样的

创建的弹出菜单
var wrapper = new ContextThemeWrapper(activity, Resource.Style.PopupMenu);
var popup = new PopupMenu(wrapper, _button);
popup.MenuInflater.Inflate(Resource.Menu.popup_menu, popup.Menu);

PopupMenu风格

<style name="PopupMenu" parent="Widget.AppCompat.PopupMenu">
    <item name="android:textColor">@color/Graphite</item>
    <item name="android:dropDownSelector">@drawable/popup_menu_selector</item>
</style>

popup_menu_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:drawable="@drawable/popup_menu_item_pressed" />
    <item android:state_selected="true" android:drawable="@drawable/popup_menu_item_pressed" />
    <item android:state_pressed="true" android:drawable="@drawable/popup_menu_item_pressed" />
</selector>

popup_menu_item_pressed

 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" android:padding="90dp">
   <solid android:color="@color/Aqua"/>
 </shape>

这是工作,按下的颜色仍为橙色:(

1 个答案:

答案 0 :(得分:0)

这是我的代码工作正常:

        int themeID = MyApp.getActTheme(m_context);
        ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(m_context, MyApp.getAttributedStyle4Theme(m_context, themeID, R.attr.mainMenuStyle_txt ));
        MyPopupMenu popup = new MyPopupMenu(contextThemeWrapper, v);
        popup.inflate(R.menu.text_display_menu);
        popup.show();

themes.xml 文件:

        <item name="mainMenuStyle_txt">@style/DarkPopupMenuTXT</item>

<style name="DarkPopupMenuTXT" parent="@style/Theme.AppCompat">
    <item name="android:overlapAnchor">true</item>
    <item name="android:colorBackground">@color/hometabs_back_dark</item>
    <item name="android:itemBackground">@drawable/mmenu_text_bg_d</item>
    <item name="android:textColor">#9a9dac</item>
</style>

按下的背景颜色在@ drawable / mmenu_text_bg_d中定义:

   <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime" >
    <item android:state_pressed="false"
        android:state_focused="true"
        android:drawable="@drawable/pressed_background_txt_menu_dark" />
    <item android:state_pressed="true"
        android:drawable="@drawable/pressed_background_txt_menu_dark" />
    <item android:drawable="@android:color/transparent" />
</selector>

@ drawable / pressed_background_txt_menu_dark:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@color/green" />
</shape>