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

Maa*_*ers 10 android background colors button android-actionbar

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

fll*_*llo 21

你需要声明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>  
Run Code Online (Sandbox Code Playgroud)

因此,将自己的drawable与a selector和每个状态(按下,聚焦,禁用等)放在一起,以获得预期的背景.例如,ab_item_background.xml上面声明的drawable 可能是这样的:

<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>
Run Code Online (Sandbox Code Playgroud)

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