How to change background color of overflow menu (p

2019-08-29 07:59发布

问题:

I am using android compact library for building the action bar for both older and newer versions of android. so my requirement includes that i need to show action bar in blue color. now i have changed the action bar background color to blue , now i want to change the background color of popup menu which comes when we click on overflow icon. I tried in many ways but nothing is changing the background color.any one suggest me whether we can change the background color of popup menu using app-compact library or not, if we can please suggest me

回答1:

Add this to your toolbar element

app:popupTheme="@style/ThemeOverlay.YourApp"

Then in your styles.xml define the popup menu style

<style name="ThemeOverlay.YourApp" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:colorBackground">@color/your_background_color</item>
    <item name="android:textColor">@color/your_text_color</item>
</style>

Note that you need to use colorBackground and never background. The latter would be applied to everything (the menu itself and each menu item), the former applies only to the popup menu.

Source: Style appcompat-v7 Toolbar menu background



回答2:

Use android:popupBackground into styles.

like

<style name ="MyPopupMenu" parent="Theme.AppCompat.Light">
    <item name="popupMenuStyle">@style/MyPopupMenuStyle</item>
</style>

<style name="MyPopupMenuStyle">
    <item name="android:dropDownSelector">@drawable/abc_list_selector_holo_dark</item>
    <item name="android:popupBackground">@drawable/abc_menu_dropdown_panel_holo_dark</item>
    <item name="android:dropDownVerticalOffset">0dip</item>
    <item name="android:dropDownHorizontalOffset">0dip</item>
    <item name="android:dropDownWidth">wrap_content</item>
</style>