ActionBarCompat支持库android:selectableItemBackground无法使用

3

我正在使用新的ActionBarCompat支持库。
当我按下操作栏中的操作按钮时,按钮的背景应该发生变化,它在Android 4.3上可以工作,但在Gingerbread上无法实现。 在Gingerbread上,如果我按下一个按钮,它不会改变背景。 我甚至已经更改了选择器:

<style name="Theme.MyCustomTheme" parent="@style/Theme.AppCompat.Light">
    <item name="selectableItemBackground">@drawable/actionbar_item_bg_selector</item>
</style>

这个功能在安卓4.3上能够正常工作,但是在Gingerbread上不能正常工作。

这是一个bug吗?

1个回答

1

我找出了问题所在。你应该复制Android选择器并进行修改。

styles.xml

<style name="Theme.NewTransaction" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="selectableItemBackground">@drawable/actionbar_item_bg_selector</item>
</style>

actionbar_item_bg_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_window_focused="false" android:drawable="@color/transparent" />

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
    <item android:state_focused="true"  android:state_enabled="false" android:state_pressed="true" android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"  android:state_enabled="false"                              android:drawable="@drawable/list_selector_background_disabled" />
    <item android:state_focused="true"                                android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="false"                               android:state_pressed="true" android:drawable="@drawable/list_selector_background_transition" />
    <item android:state_focused="true"                                                             android:drawable="@drawable/list_selector_background_focused" />
    <item android:drawable="@color/transparent" />

</selector>

我认为我的问题与这个评论有关:
尽管这两个指向相同的资源,但它们有两种状态,因此当从按下状态退出时,可绘制对象将使自身无效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接