首页 文章

如何在Android 4.2中更改Action Bar选项菜单的背景颜色?

提问于
浏览
59

我想更改Android 4.2中选项(溢出)菜单的背景颜色 . 我已经尝试了所有方法,但它仍然显示主题设置的默认颜色 . 我使用了以下代码和XML配置 .

MainActivity.java

public class MainActivity extends Activity {

@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getActionBar().setIcon(R.drawable.ic_launcher);     
    getActionBar().setTitle("Sample Menu");
    getActionBar().setBackgroundDrawable(new 
               ColorDrawable(Color.parseColor("#33B5E5"))); 

    int titleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
    TextView titleText = (TextView)findViewById(titleId);
    titleText.setTextColor(Color.parseColor("#ffffff"));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    setMenuBackground();
    return true;
}

protected void setMenuBackground(){                     
    // Log.d(TAG, "Enterting setMenuBackGround");  
    getLayoutInflater().setFactory( new Factory() { 

        @Override
        public View onCreateView(String name, Context context,
                AttributeSet attrs) {
            if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {
                try { // Ask our inflater to create the view  
                    LayoutInflater f = getLayoutInflater();  
                    final View view = f.createView( name, null, attrs );  
                    /* The background gets refreshed each time a new item is added the options menu.  
                    * So each time Android applies the default background we need to set our own  
                    * background. This is done using a thread giving the background change as runnable 
                    * object */
                    new Handler().post( new Runnable() {  
                        public void run () {  
                            // sets the background color   
                            view.setBackgroundResource( R.color.menubg);
                            // sets the text color              
                            ((TextView) view).setTextColor(Color.WHITE);
                            // sets the text size              
                            ((TextView) view).setTextSize(18);
            }
                    } );  
                return view;
            }
        catch ( InflateException e ) {}
        catch ( ClassNotFoundException e ) {}  
    } 
            return null;
        }});
}

}

Menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

<item
    android:id="@+id/action_settings"
    android:icon="@drawable/menu"
    android:showAsAction="always"
    android:title="@string/action_settings">
    <menu>
        <item
            android:id="@+id/item1"             
            android:showAsAction="always"
            android:title="@string/item1" />
        <item
            android:id="@+id/item2"             
            android:showAsAction="always"
            android:title="@string/item2" />
        <item
            android:id="@+id/item3"
            android:showAsAction="always"
            android:title="@string/item3" />
        <item
            android:id="@+id/item4"
            android:showAsAction="always"
            android:title="@string/item4" />
    </menu>
</item>

</menu>

color.xml

<color name="menubg">#33B5E5</color>

The above setMenuBackground is not taking any effect:

Menu Sample

在上图中, I want to change the menu background from black to the blue color in the Action Bar . 我怎样才能做到这一点,我做错了什么?

15 回答

  • 87

    有一种简单的方法可以更改操作栏中的颜色使用ActionBar Generator并复制粘贴 res 文件夹中的所有文件,并在Android.manifest文件中更改主题 .

  • 77

    Action Bar Style Generatorsuggested by Sunny非常有用,但它会生成 a lot 个文件,如果您只想更改背景颜色,则大多数文件都无关紧要 .

    因此,我深入研究了它生成的zip,并试图缩小重要的部分,因此我可以对我的应用程序进行最少量的更改 . 以下是我发现的 .


    在样式生成器中,相关设置为 Popup color ,这会影响“ Overflow menu ,子菜单和微调器面板背景” .

    enter image description here

    继续生成zip,但是在生成的所有文件中,你只需要一个图像, menu_dropdown_panel_example.9.png ,看起来像这样:

    enter image description here

    因此,将其不同的分辨率版本添加到 res/drawable-* . (也许可以将它们重命名为 menu_dropdown_panel.9.png . )

    然后,作为示例,在 res/values/themes.xml 中,您将拥有以下内容,其中 android:popupMenuStyleandroid:popupBackground 是关键设置 .

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

    当然,在 AndroidManifest.xml

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

    这个设置你得到了什么:

    enter image description here

    请注意,我使用 Theme.Holo.Light 作为基本主题 . 如果你使用 Theme.Holo (Holo Dark),还需要一个额外的步骤this answer describes

    此外,如果你(像我一样)想要设置整个Action Bar的样式,而不仅仅是菜单,请在_1545579中输入这样的内容:

    <!-- 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>
    

    至少在Android 4.0(API级别14)上运行良好 .

  • 17

    如果人们仍在访问工作解决方案,这对我有用: - 这适用于Appcompat支持库 . 这是ActionBar造型的延续explained here

    以下是styles.xml文件 .

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

    这就是它的外观 - MenuItem背景颜色是天蓝色,MenuItem文本颜色是粉红色,文本大小为25sp: -

    enter image description here

  • 15

    如果您阅读本文,则's probably because all the previous answers didn'适用于基于 Holo Dark 的主题 .

    Holo Dark为PopupMenus使用了一个额外的包装器,因此在执行what Jonik suggested之后,您必须将以下样式添加到'xml'文件中:

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

    然后在主题块中引用它:

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

    而已!

  • 2

    我在弹出菜单/选项菜单中更改文本背景颜色和颜色的简单方法

    <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>
    
  • 1

    您可以按照以下方式在Overflow MenuItem中应用样式和主题 . OverFlow菜单是ListView所以,我们可以按照列表视图应用主题 .

    在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

    如果你在android studio的最新工具栏中工作,那么这是最小的解决方案要更改工具栏选项菜单颜色,请将其添加到工具栏元素

    app:popupTheme="@style/MyDarkToolbarStyle"
    

    然后在styles.xml中定义弹出菜单样式

    <style name="MyDarkToolbarStyle" parent="ThemeOverlay.AppCompat.Light"> <item name="android:colorBackground">@color/mtrl_white_100</item> <item name="android:textColor">@color/mtrl_light_blue_900</item> </style>

    请注意,您需要使用colorBackground而不是背景 . 后者将应用于所有内容(菜单本身和每个菜单项),前者仅适用于弹出菜单 .

  • 3

    我也遇到了同样的问题,最后我得到了简单的解决方案 . 刚添加一行到动作栏样式 .

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
        <item name="android:textColorPrimary">@color/colorAccent</item>
        <item name="android:colorBackground">@color/colorAppWhite</item>
    </style>
    

    "android:colorBackground" 足以更改选项菜单背景

  • 0

    我只需将十六进制值设置为:就能改变动作溢出的颜色:

    <!-- 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>
    
  • 3

    快点!

    styles.xml
    
    <style name="popupTheme" parent="Theme.AppCompat.Light">
    
        <item name="android:background">@color/colorBackground</item>
        <item name="android:textColor">@color/colorItem</item>
    
    </style>
    

    然后将此特定样式添加到AppTheme样式中

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        ...
        <item name="popupTheme">@style/popupTheme</item>
    </style>
    

    DONE!

  • 0

    试试这个代码 . 将此代码段添加到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>
    

    在Manifest.xml中,在应用程序下添加以下代码段

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
    
         android:theme="@style/AppTheme"  >
    
  • 2

    将以下内容添加到 Styles.xml

    <style name="Custom_Theme" parent="Theme.AppCompat.Light.DarkActionBar">
    
        <item name="android:itemBackground">@color/white</item>
        <item name="android:textColor">@color/colorPrimaryDark</item>
    
    </style>
    

    仅在 activity_main.xml 中更改主题 .

    android:theme="@style/Custom_Theme"
    
  • 1

    以下是主题中更改操作栏和溢出菜单背景颜色所需的更改 . 你需要配置actionBarStyle和popupMenuStyle的“android:background”

    <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>
    
  • 3
    <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>
    
  • 19

    在您的应用主题中,您可以设置android:itemBackground属性以更改操作菜单的颜色 .

    例如:

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

相关问题