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

后端 未结 17 1084
别那么骄傲
别那么骄傲 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:49

    I was able to change colour of action overflow by just putting hex value at:

        <!-- The beef: background color for Action Bar overflow menu -->
    <style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
        <item name="android:popupBackground">HEX VALUE OF COLOR</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 16:49

    Try this code. Add this snippet to your res>values>styles.xml

    <style name="AppTheme" parent="AppBaseTheme">
        <item name="android:actionBarWidgetTheme">@style/Theme.stylingactionbar.widget</item>
    </style>
    <style name="PopupMenu" parent="@android:style/Widget.Holo.ListPopupWindow">
        <item name="android:popupBackground">@color/DarkSlateBlue</item>
     <!-- for @color you have to create a color.xml in res > values -->
    </style>
    <style name="Theme.stylingactionbar.widget" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/PopupMenu</item>
    </style>
    

    And in Manifest.xml add below snippet under application

     <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
    
         android:theme="@style/AppTheme"  >
    
    0 讨论(0)
  • 2020-11-22 16:50

    Following are the changes required in your theme for changing action bar & overflow menu background color. You need to configure "android:background" of actionBarStyle & popupMenuStyle

    <application
                android:name="...."
                android:theme="@style/AppLightTheme">
    
    <style name="AppLightTheme" parent="android:Theme.Holo.Light">      
            <item name="android:actionBarStyle">@style/ActionBarLight</item>
            <item name="android:popupMenuStyle">@style/ListPopupWindowLight</item>
    </style>
    
    <style name="ActionBarLight" parent="android:style/Widget.Holo.Light.ActionBar">
            <item name="android:background">@color/white</item>
    </style>
    
    
    <style name="ListPopupWindowLight"parent="@android:style/Widget.Holo.Light.ListPopupWindow">
            <item name="android:background">@color/white</item>
    </style>
    
    0 讨论(0)
  • 2020-11-22 16:51

    Within your app theme you can set the android:itemBackground property to change the color of the action menu.

    For example:

    <style name="AppThemeDark" parent="Theme.AppCompat.Light.DarkActionBar">
            <item name="colorPrimary">@color/drk_colorPrimary</item>
            <item name="colorPrimaryDark">@color/drk_colorPrimaryDark</item>
            <item name="colorAccent">@color/drk_colorAccent</item>
            <item name="actionBarStyle">@style/NoTitle</item>
            <item name="windowNoTitle">true</item>
            <item name="android:textColor">@color/white</item>
    
            <!-- THIS IS WHERE YOU CHANGE THE COLOR -->
            <item name="android:itemBackground">@color/drk_colorPrimary</item>
    </style>

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

    If you read this, it's probably because all the previous answers didn't work for your Holo Dark based theme.

    Holo Dark uses an additional wrapper for the PopupMenus, so after doing what Jonik suggested you have to add the following style to your 'xml' file:

    <style name="PopupWrapper" parent="@android:style/Theme.Holo">
        <item name="android:popupMenuStyle">@style/YourPopupMenu</item>
    </style>
    

    Then reference it in your theme block:

    <style name="Your.cool.Theme" parent="@android:style/Theme.Holo">
        .
        .
        .
        <item name="android:actionBarWidgetTheme">@style/PopupWrapper</item>
    </style>
    

    That's it!

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

    I used this and it works just fine.

    Apply this code in the onCreate() function

    val actionBar: android.support.v7.app.ActionBar? = supportActionBar
        actionBar?.setBackgroundDrawable(ColorDrawable(Color.parseColor("Your color code here")))    
    
    0 讨论(0)
提交回复
热议问题