简体   繁体   English

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

[英]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. 我正在Android上进行开发,但尚未找到一种方法来更改操作栏中项目的背景颜色。 I found ways to do with Sherlock and holo but still I can not find it working. 我找到了与Sherlock和holo有关的方法,但仍然找不到它。 I'm using this AppCompat and my styles.xml file 我正在使用此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>

For example, the MEGA app: 例如,MEGA应用程序:

Mega app 超级应用

Thanks a lot! 非常感谢!

You need to style the 'ActionButton'. 您需要设置“ 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 . 我应该注意,您使用的drawable应该是StateListDrawable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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