繁体   English   中英

按下时更改操作栏按钮背景颜色

[英]Change actionbar button background color when pressed

如果我按下操作栏中的按钮,那么它的背景颜色不是我想要的。 我的项目的背景颜色不响应我的点击事件。 我如何更改它并在按下时更改背景颜色?

您需要声明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>  

因此,将您自己的 drawable 与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>

设置操作栏样式 中,您可以找到所有可能的自定义和所有属性。

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#0a0a0a")));

这可能有帮助

暂无
暂无

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

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