我有一个RecyclerView
,ConstraintLayout
作为每个项目的根View
。
我将ConstraintLayout
背景设为selector
,selector
定义为:
<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