我正在用这个教程 http://www.androidpeople.com/android-custom-listview-tutorial-example-part-2/我想为此列表视图添加选择器。我尝试了一些代码,但它不起作用。我该怎么做。
我使用的代码为 list_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
<item android:state_pressed="true"
android:drawable="@drawable/list_selector_background_pressed" />
</selector>
和我的列表视图
<ListView
android:id="@+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="@drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
只需将此处的一个单词从 listSelector 更改为 background 即可。因为你希望它作为背景可绘制选择器,对吧?
<ListView
android:id="@+id/select_names_tags_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/list_selector"
android:layout_alignParentTop="false"
android:layout_gravity="center_vertical">
</ListView>
EDIT:
尝试将选择器文件更改为(我更改了两项的顺序):
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/list_selector_background_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/list_selector_background_focus" />
</selector>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)