我想设置的功能是,当点击列表项时,列表项的颜色会改变。
我按以下的方法设计:
list_item_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
Selected
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="@drawable/list_focused"/>
Pressed
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="@drawable/list_selected" />
</selector>
在colors.xml 中设置颜色
<drawable name="list_focused">#36C170</drawable>
<drawable name="list_selected">#9EC136</drawable>
ListView
<ListView
android:id="@+id/list_centers_complete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="@android:color/transparent"
android:listSelector="@drawable/list_item_selector" />
但是当我点击列表项时,整个背景颜色都变了,而单个列表项的颜色没变。
怎么处理这个问题啊?
在列表视图的列中应用"@drawable/list_item_selector"
本身就不是一个列表
你的列表项类似于
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@drawable/list_item_selector">
<TextView android:id="@+id/textForList"
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:padding="10sp" />
.
.
.
</LinearLayout>
list_item_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true">
<shape>
<solid android:color="#66FFFFFF" />
</shape>
</item>
<item>
<shape>
<solid android:color="#FF666666" />
</shape>
</item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/list_selected" />
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="@drawable/list_focused"/>
<item
android:drawable="@drawable/list_normal" />
</selector>
使用:
android:background="@drawable/list_item_selector""