首页 > 代码库 > 点击之后变色,还切换背景

点击之后变色,还切换背景

1,新建xml文件存放到drawble目录下

切换颜色

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

<item android:color="@color/black" android:state_checked="true"></item>
<item android:color="@color/black" android:state_selected="true"></item>
<item android:color="@color/black" android:state_pressed="true"></item>
<item android:color="@color/gray" android:state_checked="false"></item>
<item android:color="@color/gray"></item>

</selector>

 

切换背景图片

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

<item android:drawable="@drawable/guide_home_on" android:state_pressed="true"/>
<item android:drawable="@drawable/guide_home_on" android:state_checked="true"/>
<item android:drawable="@drawable/guide_home_on" android:state_selected="true"/>
<item android:drawable="@drawable/guide_home_nm" android:state_checked="false"/>
<item android:drawable="@drawable/guide_home_nm"/>

</selector>

2,在布局文件中引用

android:textColor="@drawable/radio_text_selector"

点击之后变色,还切换背景