您的位置:首页 > 移动开发 > Android开发

android开发之给LinearLayout增加点击效果

2016-01-08 22:58 561 查看
场景: 给LinearLayout设置android:background="@drawable/ll_customer_selector",却没有效果

解决办法:给LinearLayout增加android:clickable="true"属性,是它可点击

示例代码如下:

在布局中xxx.xml中

<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:android="http://schemas.android.com/apk/res/android">

<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:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#ffffff" />

</shape>
在drawable下的ll_customer_selected中

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

<solid android:color="#DCDCDC" />

</shape>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: