[英]How to change the background color of an item when pressed on the action bar?
我正在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應用程序:
非常感謝!
您需要設置“ 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 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.