简体   繁体   中英

How to change the background color of an item when pressed on the action bar?

I'm developing on Android, and I have not yet found a way to change the background color of an item in the action bar. I found ways to do with Sherlock and holo but still I can not find it working. I'm using this AppCompat and my styles.xml file

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

<style name="AppTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="actionBarItemBackground">@drawable/selectable_background_apptheme</item>
    <item name="popupMenuStyle">@style/PopupMenu.Apptheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Apptheme</item>
    <item name="actionBarTabStyle">@style/ActionBarTabStyle.Apptheme</item>
    <item name="actionDropDownStyle">@style/DropDownNav.Apptheme</item>
    <item name="actionBarStyle">@style/ActionBar.Solid.Apptheme</item>
    <item name="actionModeBackground">@drawable/cab_background_top_apptheme</item>
    <item name="actionModeSplitBackground">@drawable/cab_background_bottom_apptheme</item>
    <item name="actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Apptheme</item>

    <!-- Light.DarkActionBar specific -->
    <item name="actionBarWidgetTheme">@style/Theme.Apptheme.Widget</item>

    <item name="android:actionBarItemBackground">@drawable/selectable_background_apptheme</item>
    <item name="android:popupMenuStyle">@style/PopupMenu.Apptheme</item>
    <item name="android:dropDownListViewStyle">@style/DropDownListView.Apptheme</item>
    <item name="android:actionBarTabStyle">@style/ActionBarTabStyle.Apptheme</item>
    <item name="android:actionDropDownStyle">@style/DropDownNav.Apptheme</item>
    <item name="android:actionBarStyle">@style/ActionBar.Solid.Apptheme</item>
    <item name="android:actionModeBackground">@drawable/cab_background_top_apptheme</item>
    <item name="android:actionModeSplitBackground">@drawable/cab_background_bottom_apptheme</item>
    <item name="android:actionModeCloseButtonStyle">@style/ActionButton.CloseMode.Apptheme</item>

</style>

<style name="ActionBar.Solid.Apptheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="background">@drawable/ab_solid_apptheme</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_apptheme</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_apptheme</item>
    <item name="progressBarStyle">@style/ProgressBar.Apptheme</item>

    <item name="android:background">@drawable/ab_solid_apptheme</item>
    <item name="android:backgroundStacked">@drawable/ab_stacked_solid_apptheme</item>
    <item name="android:backgroundSplit">@drawable/ab_bottom_solid_apptheme</item>
    <item name="android:progressBarStyle">@style/ProgressBar.Apptheme</item>
</style>

<style name="ActionBar.Transparent.Apptheme" parent="@style/Widget.AppCompat.ActionBar">
    <item name="background">@drawable/ab_transparent_apptheme</item>
    <item name="progressBarStyle">@style/ProgressBar.Apptheme</item>

    <item name="android:background">@drawable/ab_transparent_apptheme</item>
    <item name="android:progressBarStyle">@style/ProgressBar.Apptheme</item>
</style>

<style name="PopupMenu.Apptheme" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_apptheme</item>
</style>

<style name="DropDownListView.Apptheme" parent="@style/Widget.AppCompat.ListView.DropDown">
    <item name="android:listSelector">@drawable/selectable_background_apptheme</item>
</style>

<style name="ActionBarTabStyle.Apptheme" parent="@style/Widget.AppCompat.ActionBar.TabView">
    <item name="android:background">@drawable/tab_indicator_ab_apptheme</item>
</style>

<style name="DropDownNav.Apptheme" parent="@style/Widget.AppCompat.Spinner.DropDown.ActionBar">
    <item name="android:background">@drawable/spinner_background_ab_apptheme</item>
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_apptheme</item>
    <item name="android:dropDownSelector">@drawable/selectable_background_apptheme</item>
</style>

<style name="ProgressBar.Apptheme" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressDrawable">@drawable/progress_horizontal_apptheme</item>
</style>

<style name="ActionButton.CloseMode.Apptheme" parent="@style/Widget.AppCompat.ActionButton.CloseMode">
    <item name="android:background">@drawable/btn_cab_done_apptheme</item>
</style>

<!-- this style is only referenced in a Light.DarkActionBar based theme -->
<style name="Theme.Apptheme.Widget" parent="@style/Theme.AppCompat">
    <item name="popupMenuStyle">@style/PopupMenu.Apptheme</item>
    <item name="dropDownListViewStyle">@style/DropDownListView.Apptheme</item>

    <item name="android:popupMenuStyle">@style/PopupMenu.Apptheme</item>
    <item name="android:dropDownListViewStyle">@style/DropDownListView.Apptheme</item>
</style>

</resources>

For example, the MEGA app:

Mega app

Thanks a lot!

You need to style the 'ActionButton'.

<style name="Widget.AppCompat.ActionButton.AppTheme"
       parent="Widget.AppCompat.Light.Base.ActionButton">
    <item name="android:background">@drawable/your_background</item>
    <item name="background">@drawable/your_background</item>
</style>

<style name="ActionBar.Solid.Apptheme" parent="@style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    //Add this line
    <item name="actionButtonStyle">@style/Widget.AppCompat.ActionButton.AppTheme</item>

    <item name="background">@drawable/ab_solid_apptheme</item>
    <item name="backgroundStacked">@drawable/ab_stacked_solid_apptheme</item>
    <item name="backgroundSplit">@drawable/ab_bottom_solid_apptheme</item>
    <item name="progressBarStyle">@style/ProgressBar.Apptheme</item>

    //Add this line
    <item name="android:actionButtonStyle">@style/Widget.AppCompat.ActionButton.AppTheme</item>

    <item name="android:background">@drawable/ab_solid_apptheme</item>
    <item name="android:backgroundStacked">@drawable/ab_stacked_solid_apptheme</item>
    <item name="android:backgroundSplit">@drawable/ab_bottom_solid_apptheme</item>
    <item name="android:progressBarStyle">@style/ProgressBar.Apptheme</item>

I should note that the drawable you use should be a StateListDrawable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM