场景: 给LinearLayout设置android:background="@drawable/ll_customer_selector",却没有效果
解决办法:给LinearLayout增加android:clickable="true"属性,是它可点击
示例代码如下:
在布局中中
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/ll_customer_selector"
android:clickable="true"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="收藏"
android:textSize="12sp" />
</LinearLayout>
在drawable下的ll_customer_selector.xml中
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:andro>
<item android:drawable="@drawable/ll_customer_selected" android:state_pressed="true"/>
<!-- focused state -->
<item android:drawable="@drawable/ll_customer_selected" android:state_focused="true"/>
<!-- default state -->
<item android:drawable="@drawable/ll_customer_normal"/>
</selector>
在drawable下的ll_customer_normal中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro >
<solid android:color="#ffffff" />
</shape>
在drawable下的ll_customer_selected中
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:andro >
<solid android:color="#DCDCDC" />
</shape>