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

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

本文介绍了按下时更改操作栏按钮的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

需要声明​​android:actionBarItemBackground 属性是:

You need to declare android:actionBarItemBackground attribute which is a:

操作栏项目的自定义项目状态列表可绘制背景.

然后,在您的样式中执行以下操作:

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>

因此,将您自己的可绘制对象与 selector 和每个状态(按下、聚焦、禁用等)一起放入预期的背景.例如,上面声明的可绘制 ab_item_background.xml 可能是这样的:

So, put your own drawable with a selector and every state (pressed, focused, disabled, etc) to have the expected background. For example, the drawable ab_item_background.xml declared above might be like this:

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

这篇关于按下时更改操作栏按钮的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:33