<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"