如果我在操作栏中按下一个按钮,那么它的背景颜色不是我想要的。我的项目的背景颜色没有响应我的点击事件。如何更改此并在按下时更改背景颜色?
您需要声明 android:actionBarItemBackground
属性,它是以下内容:
用于操作栏项目的自定义项目状态列表可绘制背景。
然后,在您的样式中按照以下方式操作:
<style name="CustomStyle" parent="@style/Theme.Holo.Light" >
<item name="android:actionBarItemBackground">@drawable/ab_item_background</item>
<item name="actionBarItemBackground">@drawable/ab_item_background</item>
</style>
因此,使用selector
和每个状态(按下、聚焦、禁用等)放置您自己的可绘制对象,以获得预期的背景。例如,上面声明的ab_item_background.xml
可像这样:
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="@android:integer/config_mediumAnimTime">
<!-- focused/pressed: color=red -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/red" />
<!-- pressed: color=red -->
<item
android:state_pressed="true"
android:drawable="@color/red" />
<!-- normal: color=transparent -->
<item
android:drawable="@android:color/transparent" />
</selector>
在美化Action Bar中,您可以找到所有定制可能性以及实现它们所需的所有属性。
android:actionBarItemBackground
,而 actionBarItemBackground
则会出错,所以我把它删除了,这两个都是必须的吗? - Rohan KandwalActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));