简体   繁体   English

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

[英]Change actionbar button background color when pressed

If I press a button in the action bar, then its background color is not what I want.如果我按下操作栏中的按钮,那么它的背景颜色不是我想要的。 The background color of my item doesn't respond to my click event.我的项目的背景颜色不响应我的点击事件。 How can I change this and change the background color when it's pressed?我如何更改它并在按下时更改背景颜色?

You need to declare android:actionBarItemBackground attribute which is a:您需要声明android:actionBarItemBackground属性,它是一个:

Custom item state list drawable background for action bar items.操作栏项目的自定义项目状态列表可绘制背景。

Then, in your styles do as follows:然后,在您的样式中执行以下操作:

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

So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background.因此,将您自己的 drawable 与selector和每个状态(按下、聚焦、禁用等)放在一起,以获得预期的背景。 For example, the drawable ab_item_background.xml declared above might be like this:例如,上面声明的可绘制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>

In Styling the Action Bar , you can find all the customization possibles and all the attributes to do so.设置操作栏样式 中,您可以找到所有可能的自定义和所有属性。

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

this might help这可能有帮助

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

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