How to change the background color of Action Bar's Option Menu in Android 4.2?

后端 未结 17 1108
别那么骄傲
别那么骄傲 2020-11-22 16:00

I\'d like to change the background color of the option (overflow) menu in Android 4.2. I have tried all the methods but it is still showing the default color set by the them

相关标签:
17条回答
  • 2020-11-22 16:39
    <style name="customTheme" parent="any_parent_theme">
        <item name="android:itemBackground">#424242</item>
        <item name="android:itemTextAppearance">@style/TextAppearance</item>
    </style>
    
    <style name="TextAppearance">
        <item name="android:textColor">#E9E2BF</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 16:44

    You can apply styles and Themes in Overflow MenuItem as per below. OverFlow Menu is ListView so, we can apply theme as per listview.

    Apply below code in styles.xml

    <style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <item name="android:dropDownListViewStyle">@style/PopupMenuListView</item>
            <item name="android:actionBarWidgetTheme">@style/PopupMenuTextView</item>
            <item name="android:popupMenuStyle">@style/PopupMenu</item>
            <item name="android:listPreferredItemHeightSmall">40dp</item>
    </style>
    
    <!-- Change Overflow Menu ListView Divider Property -->
        <style name="PopupMenuListView" parent="@android:style/Widget.Holo.ListView.DropDown">
            <item name="android:divider">@color/app_navigation_divider</item>
            <item name="android:dividerHeight">1sp</item>
            <item name="android:listSelector">@drawable/list_selector</item>
        </style>
    
        <!-- Change Overflow Menu ListView Text Size and Text Size -->
        <style name="PopupMenuTextView" parent="@android:style/Widget.Holo.Light.TextView">
            <item name="android:textColor">@color/app_white</item>
            <item name="android:textStyle">normal</item>
            <item name="android:textSize">18sp</item>
            <item name="android:drawablePadding">25dp</item>
            <item name="android:drawableRight">@drawable/navigation_arrow_selector</item>
        </style>
    
        <!-- Change Overflow Menu Background -->
        <style name="PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
            <item name="android:popupBackground">@drawable/menu_overflow_bg</item>
        </style>
    
    0 讨论(0)
  • 2020-11-22 16:45

    My simple trick to change background color and color of the text in Popup Menu / Option Menu

    <style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/MyPopupMenu</item>
        <item name="android:itemTextAppearance">@style/TextAppearance</item>
    </style>
    <!-- Popup Menu Background Color styles -->
    <style name="MyPopupMenu" 
           parent="@android:style/Widget.Holo.ListPopupWindow">
        <item name="android:popupBackground">@color/Your_color_for_background</item> 
    </style>
    <!-- Popup Menu Text Color styles -->
    <style name="TextAppearance">
        <item name="android:textColor">@color/Your_color_for_text</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 16:46

    There is an easy way to change the colors in Actionbar Use ActionBar Generator and copy paste all file in your res folder and change your theme in Android.manifest file.

    0 讨论(0)
  • 2020-11-22 16:46

    The Action Bar Style Generator, suggested by Sunny, is very useful, but it generates a lot of files, most of which are irrelevant if you only want to change the background colour.

    So, I dug deeper into the zip it generates, and tried to narrow down what are the parts that matter, so I can make the minimum amount of changes to my app. Below is what I found out.


    In the style generator, the relevant setting is Popup color, which affects "Overflow menu, submenu and spinner panel background".

    enter image description here

    Go on and generate the zip, but out of all the files generated, you only really need one image, menu_dropdown_panel_example.9.png, which looks something like this:

    enter image description here

    So, add the different resolution versions of it to res/drawable-*. (And perhaps rename them to menu_dropdown_panel.9.png.)

    Then, as an example, in res/values/themes.xml you would have the following, with android:popupMenuStyle and android:popupBackground being the key settings.

    <resources>
    
        <style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
            <item name="android:popupMenuStyle">@style/MyApp.PopupMenu</item>
            <item name="android:actionBarStyle">@style/MyApp.ActionBar</item>
        </style>
    
        <!-- The beef: background color for Action Bar overflow menu -->
        <style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
            <item name="android:popupBackground">@drawable/menu_dropdown_panel</item>
        </style>
    
        <!-- Bonus: if you want to style whole Action Bar, not just the menu -->
        <style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">
            <!-- Blue background color & black bottom border -->
            <item name="android:background">@drawable/blue_action_bar_background</item>
        </style>   
    
    </resources>
    

    And, of course, in AndroidManifest.xml:

    <application
        android:theme="@style/MyAppActionBarTheme" 
        ... >
    

    What you get with this setup:

    enter image description here

    Note that I'm using Theme.Holo.Light as the base theme. If you use Theme.Holo (Holo Dark), there's an additional step needed as this answer describes!

    Also, if you (like me) wanted to style the whole Action Bar, not just the menu, put something like this in res/drawable/blue_action_bar_background.xml:

    <!-- Bonus: if you want to style whole Action Bar, not just the menu -->
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    
        <item>
            <shape android:shape="rectangle">
                <stroke android:width="2dp" android:color="#FF000000" />
                <solid android:color="#FF2070B0" />                   
            </shape>
        </item>
    
        <item android:bottom="2dp">
            <shape android:shape="rectangle">
                <stroke android:width="2dp" android:color="#FF2070B0" />
                <solid android:color="#00000000" />
                <padding android:bottom="2dp" />
            </shape>
        </item>
    
    </layer-list>
    

    Works great at least on Android 4.0+ (API level 14+).

    0 讨论(0)
  • 2020-11-22 16:49

    In case people are still visiting for a working solution, here is what worked for me:-- This is for Appcompat support library. This is in continuation to ActionBar styling explained here

    Following is the styles.xml file.

    <resources>
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light">
            <!-- This is the styling for action bar -->
            <item name="actionBarStyle">@style/MyActionBar</item>
            <!--To change the text styling of options menu items</item>-->
            <item name="android:itemTextAppearance">@style/MyActionBar.MenuTextStyle</item>
            <!--To change the background of options menu-->
            <item name="android:itemBackground">@color/skyBlue</item>
        </style>
    
        <style name="MyActionBar" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
            <item name="background">@color/red</item>
            <item name="titleTextStyle">@style/MyActionBarTitle</item>
        </style>
    
        <style name="MyActionBarTitle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">@color/white</item>
        </style>
    
        <style name="MyActionBar.MenuTextStyle"
            parent="style/TextAppearance.AppCompat.Widget.ActionBar.Title">
            <item name="android:textColor">@color/red</item>
            <item name="android:textStyle">bold</item>
            <item name="android:textSize">25sp</item>
        </style>
    </resources>
    

    and this is how it looks--MenuItem background color is skyblue and MenuItem text color is pink with textsize as 25sp:--

    0 讨论(0)
提交回复
热议问题