如何在Selector上设置selectableItemBackground Ripple

时间:2017-11-13 01:03:26

标签: android user-interface android-constraintlayout

我有一个RecyclerViewConstraintLayout作为每个项目的根View

我将ConstraintLayout背景设为selectorselector定义为:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?attr/selectableItemBackground" android:state_pressed="true" />
    <item android:drawable="@color/colorAccent" android:state_activated="true" />
    <item android:drawable="@android:color/transparent" />
</selector>

我的目标是在按下某个项目时产生涟漪效果,并在选择项目时将项目的背景更改为强调色。

问题是,selectableItemBackground适用于任何View background,但如果我将它放在选择器上,我的应用就会立即崩溃ConstraintLayout被夸大了。

如果我删除了?attr/selectableItemBackground并将其替换为任何颜色,selector就可以了。

这是Logcat上的简化崩溃错误:

android.view.InflateException: Binary XML file line #0: Binary XML file line #0: 
        Error inflating class android.support.constraint.ConstraintLayout                                                                 
Caused by: android.view.InflateException: Binary XML file line #0: 
        Error inflating class android.support.constraint.ConstraintLayout                                                                            
Caused by: java.lang.reflect.InvocationTargetException
Caused by: android.content.res.Resources$NotFoundException: 
        Drawable com.test.test:drawable/list_item_background_selector with resource ID #0x7f080086
Caused by: android.content.res.Resources$NotFoundException: 
        File res/drawable/list_item_background_selector.xml from drawable resource ID #0x7f080086

0 个答案:

没有答案