How to change ActionBar menu background color (and

2019-02-08 13:30发布

I've got one trouble while changing android design. I need to change action bar menu background color to black. I know, there are lots of answers for this question. I tried many of them. But nothing worked for me.

Oh, and i forgot to say, that i am not a very skilled android developer. Here's my styles file (v21)

<?xml version="1.0" encoding="utf-8"?>

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="colorPrimary">@color/mainColor</item>
    <item name="colorPrimaryDark">@color/secondaryColor</item>
    <item name="colorAccent">@color/goldColor</item>
    <item name="colorControlNormal">@color/mainColor</item>
    <item name="colorControlActivated">@color/secondaryColor</item>
    <item name="colorControlHighlight">@color/secondaryColor</item>
    <item name="android:navigationBarColor">@color/blackColor</item>
    <item name="colorSwitchThumbNormal">@color/secondaryTextColor</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:textColorPrimary">@color/goldColor</item>
    <item name="android:windowBackground">@color/blackColor</item>
    <item name="android:itemTextAppearance">@color/normalTextColor</item>
    <item name="android:buttonStyle">@style/ButtonStyle</item>
    <item name="android:actionMenuTextColor">@color/blackColor</item>
    <item name="actionMenuTextColor">@color/blackColor</item>
    <item name="android:actionBarPopupTheme">@style/Theme.Red.Widget</item>
    <item name="android:popupTheme">@color/blackColor</item>
</style>


<style name="Theme.Red.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/PopupMenu.Red</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Red</item>
</style>

<style name="PopupMenu.Red" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@color/blackColor</item>
</style>

<style name="DropDownListView.Red" parent="@style/Widget.AppCompat.ListView.DropDown">
    <item name="android:listSelector">@color/blackColor</item>
</style>

<color name="mainColor">#373737</color>
<color name="secondaryColor">#252525</color>
<color name="goldColor">#cea939</color>
<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/listDivider</item>
</style>
<style name="ButtonStyle" parent="android:Widget.Button">
    <item name="android:background">@drawable/custom_btn</item>
</style>

And here is my code for lower android releases.

<resources>

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
    <item name="colorPrimary">@color/mainColor</item>
    <item name="colorPrimaryDark">#856f2d</item>
    <item name="colorAccent">@color/mainColor</item>
    <item name="colorControlNormal">@color/mainColor</item>
    <item name="colorControlActivated">#856f2d</item>
    <item name="colorControlHighlight">#856f2d</item>
    <item name="colorSwitchThumbNormal">@color/mainColor</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:textColorPrimary">@color/blackColor</item>
    <item name="android:textColorSecondary">@color/blackColor</item>
    <item name="android:textColorTertiary">@color/blackColor</item>
    <item name="android:windowBackground">@color/backgroundColor</item>
    <item name="android:itemTextAppearance">@color/normalTextColor</item>

    <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
    <item name="android:textColorHighlight">#99cea939</item>
    <item name="android:textSelectHandleLeft">@drawable/apptheme_text_select_handle_left</item>
    <item name="android:textSelectHandleRight">@drawable/apptheme_text_select_handle_right</item>
    <item name="android:textSelectHandle">@drawable/apptheme_text_select_handle_middle</item>
    <item name="android:autoCompleteTextViewStyle">@style/AutoCompleteTextViewAppTheme</item>
    <item name="android:listChoiceIndicatorMultiple">@drawable/apptheme_btn_check_holo_light</item>
    <item name="android:listChoiceIndicatorSingle">@drawable/apptheme_btn_radio_holo_light</item>
    <item name="android:buttonStyle">@style/ButtonAppTheme</item>
    <item name="android:imageButtonStyle">@style/ImageButtonAppTheme</item>
    <item name="android:dropDownSpinnerStyle">@style/SpinnerAppTheme</item>
    <item name="android:progressBarStyleHorizontal">@style/ProgressBarAppTheme</item>
    <item name="android:seekBarStyle">@style/SeekBarAppTheme</item>
    <item name="android:ratingBarStyle">@style/RatingBarAppTheme</item>
    <item name="android:ratingBarStyleIndicator">@style/RatingBarBigAppTheme</item>
    <item name="android:ratingBarStyleSmall">@style/RatingBarSmallAppTheme</item>
    <item name="android:buttonStyleToggle">@style/ToggleAppTheme</item>
    <item name="android:listChoiceBackgroundIndicator">@drawable/apptheme_list_selector_holo_light</item>
    <item name="android:activatedBackgroundIndicator">@drawable/apptheme_activated_background_holo_light</item>
    <item name="android:fastScrollThumbDrawable">@drawable/apptheme_fastscroll_thumb_holo</item>

</style>

<style name="SettingsToolbarTheme" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/goldColor</item>
    <item name="actionMenuTextColor">@color/abc_primary_text_material_light</item>
    <item name="android:textColorSecondary">@color/goldColor</item>
</style>

<style name="Divider">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/listDivider</item>
</style>
<style name="AutoCompleteTextViewAppTheme" parent="android:Widget.Holo.Light.AutoCompleteTextView">
    <item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
    <item name="android:background">@drawable/apptheme_edit_text_holo_light</item>
</style>
<style name="ButtonAppTheme" parent="android:Widget.Holo.Light.Button">
    <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>
<style name="ImageButtonAppTheme" parent="android:Widget.Holo.Light.ImageButton">
    <item name="android:background">@drawable/apptheme_btn_default_holo_light</item>
</style>
<style name="SpinnerAppTheme" parent="android:Widget.Holo.Light.Spinner">
    <item name="android:background">@drawable/apptheme_spinner_background_holo_light</item>
    <item name="android:dropDownSelector">@drawable/apptheme_list_selector_holo_light</item>
</style>
<style name="TabAppTheme">
    <item name="android:gravity">center_horizontal</item>
    <item name="android:paddingLeft">16dip</item>
    <item name="android:paddingRight">16dip</item>
    <item name="android:background">@drawable/apptheme_tab_indicator_holo</item>
    <item name="android:layout_width">0dip</item>
    <item name="android:layout_weight">1</item>
    <item name="android:minWidth">80dip</item>
</style>
<style name="TabTextAppTheme">
    <item name="android:textColor">#000000</item>
    <item name="android:textSize">12sp</item>
    <item name="android:textStyle">bold</item>
    <!-- v14 <item name="android:textAllCaps">true</item> -->
    <item name="android:ellipsize">marquee</item>
    <item name="android:maxLines">2</item>
    <item name="android:maxWidth">180dip</item>
</style>
<style name="ProgressBarAppTheme" parent="android:Widget.Holo.Light.ProgressBar.Horizontal">
    <item name="android:progressDrawable">@drawable/apptheme_progress_horizontal_holo_light</item>
    <item name="android:indeterminateDrawable">@drawable/apptheme_progress_indeterminate_horizontal_holo_light</item>
</style>
<style name="SeekBarAppTheme" parent="android:Widget.Holo.Light.SeekBar">
    <item name="android:progressDrawable">@drawable/apptheme_scrubber_progress_horizontal_holo_light</item>
    <item name="android:indeterminateDrawable">@drawable/apptheme_scrubber_progress_horizontal_holo_light</item>
    <item name="android:thumb">@drawable/apptheme_scrubber_control_selector_holo_light</item>
</style>
<style name="RatingBarAppTheme" parent="android:Widget.Holo.Light.RatingBar">
    <item name="android:progressDrawable">@drawable/apptheme_ratingbar_full_holo_light</item>
    <item name="android:indeterminateDrawable">@drawable/apptheme_ratingbar_full_holo_light</item>
</style>
<style name="RatingBarBigAppTheme" parent="android:Widget.Holo.Light.RatingBar.Indicator">
    <item name="android:progressDrawable">@drawable/apptheme_ratingbar_holo_light</item>
    <item name="android:indeterminateDrawable">@drawable/apptheme_ratingbar_holo_light</item>
</style>
<style name="RatingBarSmallAppTheme" parent="android:Widget.Holo.Light.RatingBar.Small">
    <item name="android:progressDrawable">@drawable/apptheme_ratingbar_small_holo_light</item>
    <item name="android:indeterminateDrawable">@drawable/apptheme_ratingbar_small_holo_light</item>
</style>
<style name="ToggleAppTheme" parent="android:Widget.Holo.Light.Button.Toggle">
    <item name="android:background">@drawable/apptheme_btn_toggle_holo_light</item>
</style>

I would be very thankful to you for any help.

3条回答
再贱就再见
2楼-- · 2019-02-08 13:59

Background Color of menu / drop down / spinner

One of a few options:

Edit styles.xml

<resources>
<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:popupMenuStyle">@style/PopupMenu</item>
        <item name="android:popupBackground">#272727</item>
        <item name="android:itemBackground">#cc9d3e</item>
</style>

Create popup_menu.xml under res/menu/ like this:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="32dp"
android:tint="@color/timeSettings"
app:popupTheme="@style/PopupMenu">

<item
    android:id="@+id/min0"
    android:orderInCategory="100"
    android:title="0"
    app:showAsAction="never" />

</menu>

Edit popupMenu in your java-file:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_clock);

    //Time
    secBtn = (Button) findViewById(R.id.btn_sec);
    secBtn.setText("00");
    //levelBtn.setTypeface(tf1);
    secBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            PopupMenu popupMenu = new PopupMenu(MainActivityClock.this, secBtn);
            popupMenu.getMenuInflater().inflate(R.menu.popup_menu_sec, popupMenu.getMenu());
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    //Toast.makeText(MainActivity.this, "" + item.getTitle(), Toast.LENGTH_SHORT).show();
                    secBtn.setText("" + item.getTitle());
                    workTime_sec = Integer.parseInt(item.getTitle().toString());
                    //sec = item.getTitle().toString();
                    System.out.println("workTime_sec: " + workTime_sec);
                    return true;
                }
            });
            popupMenu.show();
        }
    });
}

enter image description here

查看更多
甜甜的少女心
3楼-- · 2019-02-08 14:10

add actionOverflowMenuStyle item to your app theme

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="actionOverflowMenuStyle">@style/CMOptionsMenu</item>
    </style>


<style name="CMOptionsMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
     <item name="android:popupBackground">your color code</item>
</style>
查看更多
beautiful°
4楼-- · 2019-02-08 14:11

You can simply add the line like this:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <!-- To change the background of options menu-->
    <item name="android:itemBackground">*Your_color*</item>

</style>
查看更多
登录 后发表回答