今天在项目中遇到了一个问题—— StateListDrawable失灵了。
经过反复排查发现:在使用的状态选择器的布局中,如果子View已经设置了Background,那么该布局的状态选择器将无法生效。
例子如下
状态选择器 var_bg.xml 如下 :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/red" android:state_pressed="true" />
<item android:color="@color/black" />
</selector>
错误代码
<LinearLayout android:width ="match_parent"
android:height = "wrap_content"
orientation = "vertical">
//应该将此处TextView中的backgroud去掉,才能将状态选择器生效
<TextView android:width ="match_parent"
android:height = "wrap_content"
android:background = "@color/red"/>
</LinearLayout>