问题描述
- TextView设置selector修改背景色无效
-
<item android:drawable="@color/transparent" android:state_selected="false"></item> <item android:drawable="@color/bg_item_list" android:state_selected="true"></item>
<TextView android:id="@+id/tv5" android:textColor="@color/white" android:text="本科" android:paddingLeft="25dp" android:paddingTop="10dp" android:paddingBottom="10dp" android:clickable="true" android:background="@drawable/selector_education" android:layout_width="match_parent" android:layout_height="wrap_content" />
选中状态无效,设置state_pressed点击属性有效 可是只想要选中的状态,而且不想用button,
解决方案
你这样直接写,是无效的,你应该,先将颜色抽出来,作为一个shape,然后在selector里面引用。
1.先创建shape drawable
(1)创建选中时的背景tv_selected_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/bg_item_list" />
</shape>
(2)创建默认的背景 tv_normal_bg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/transparent" />
</shape>
2.创建selector selector_education.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/ tv_normal_bg" android:state_selected="false"></item>
<item android:drawable="@drawable/tv_selected_bg" android:state_selected="true"></item>
</selector>
最后再是引用,就是你的TextView的代码。
android:background="@drawable/selector_education"
解决方案二:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
上面写的是<selector 吗
解决方案三:
<item android:drawable="@color/transparent" android:state_selected="false"></item>
<item android:drawable="@color/bg_item_list" android:state_selected="true"></item>
<item android:drawable="@color/bg_item_list" android:state_focused="true"></item>
解决方案四:
换成android:state_pressed 试试
解决方案五:
http://blog.csdn.net/shakespeare001/article/details/7788400/
解决方案六:
解决方案七:
解决方案八:
http://blog.csdn.net/huangbiao86/article/details/6707111
解决方案九:
http://blog.csdn.net/huangbiao86/article/details/6707111
时间: 2024-12-09 00:55:31