警告:此问题中的 XML 是错误的,请先阅读答案,然后再混淆!
我已经用头撞墙有一段时间了。以下帖子阐明了该主题,但未能解决我的问题:Android ListView 状态列表不显示默认项目背景 and 通过自定义选择器的 ListView 项目背景
当我选择列表项时,正确的九块背景会完美显示,但我无法获得最初显示的默认九块背景。在我看来,我需要以某种方式设置默认项目背景,但我不知道该怎么做。
列表显示:
<?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="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/shopListHeader"
/>
<ListView
android:id="@+id/shopList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0px"
android:divider="#FFFFFFFF"
android:listSelector="@drawable/shop_list_selector"
/>
</LinearLayout>
选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<!-- the list items are enabled and being pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/shop_list_item_pressed" />
<item
android:state_selected="true"
android:textColor="#FFFFFFFF" />
</selector>
背景:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<item
android:state_selected="true"
android:drawable="@android:color/transparent" />
<item
android:drawable="@drawable/shop_list_item" />
</selector>
正如您所看到的,我简化了参考文献中的示例。
您可能还注意到背景选择器没有在任何地方被引用。我开始将它扔在随机的地方(如果应用程序编译了添加则没有效果或导致强制关闭)
我还尝试在选择但未按下某个项目时阻止文本颜色变为黑色和灰色(可以通过滚动列表来完成)。由于我的背景中心为黑色,因此选择时文本将部分不可见。据我所知,该添加(选择器中的最后一个项目节点)没有任何作用。
有谁对让这个极其耗时的功能发挥作用有什么想法吗?