如何在操作栏上按下时更改项目的背景颜色?

时间:2013-12-30 22:45:49

标签: android android-actionbar android-appcompat

我正在Android上开发,我还没有找到一种方法来更改操作栏中项目的背景颜色。我找到了与Sherlock和holo有关的方法,但我仍然无法找到它。我正在使用这个AppCompat和我的styles.xml文件

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

例如,MEGA app:

Mega app

非常感谢!

1 个答案:

答案 0 :(得分:4)

你需要设置'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>

我应该注意你使用的drawable应该是StateListDrawable