我的意思是当我在ListView中单击一行时,该项的背景颜色不会变成@color/blue颜色,但在ICS中会变成。以下是我使用的选择器代码(listing_selector.xml):
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_focused="true" android:drawable="@color/blue" />
<item android:state_pressed="true" android:drawable="@color/blue" />
<item android:state_activated="true" android:drawable="@color/blue_selected" />
<item android:state_selected="true" android:drawable="@color/blue_selected" />
<item android:drawable="@android:color/transparent" />
</selector>
这是ListView
项的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:background="@color/listing_selector"
>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
这是蓝色资源:
<resources>
<color name="blue">#ff33b5e5</color>
</resources>
更新 1:
尝试将选择器从color
文件夹移动到drawable
文件夹,并将代码更新为以下内容:
android:background="@drawable/listing_selector"
更新2:
此外,在ListView
上,尝试添加了这个:
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:listSelector="@drawable/listing_selector"
/>
更新3:
我本以为是我的代码有问题,但是我已经从ListView
的onListItemClick
中删除了所有的代码,但是selector
仍然无法正常工作。
更新4:
我已经缩小范围到state_selected
或者state_activated
无法正常工作,而state_pressed
似乎可以正常工作。
更新5:
我认为我误解了,我不认为选择器被识别了。我把内置的ListView
高亮显示错认为我的选择器。我现在在想这可能与我的项目设置有关。我将选择器放在库类中。也许ICS和JB之间发生了一些变化,但是将选择器移动到我的应用程序的项目中似乎没有解决它。
更新6:
好吧,在更多的抓狂之后,我又把范围缩小到state_selected
或state_activated
不能被识别,因为改变state_pressed
的颜色确实有效,这意味着我的选择器正在被识别。根据评论,似乎是我的应用程序特定的问题,因为其他人已经能够在Jelly Bean中使用选择器。
还有一件有趣的事情是,更改默认状态下的drawable
值并没有被识别。我认为将其更改为颜色会导致列表更改为该颜色,但实际上并没有发生。
另外,在ICS中也无法正常工作。
更新7:
经过更多的抓狂之后,我发现长按菜单项会导致该项的颜色被更改。而单击某个项目仍然不起作用。甚至不确定这意味着什么。
最终更新:
我放弃了,我删除了选择器,只是在点击时刷新ListView
并记住点击的位置,并从代码中对其进行高亮显示。虽然不是理想的解决方案,但不值得为修复它而付出努力。
ListView
中的项目的蛮力方法。但我没有听到任何关于它的抱怨,所以这是一个可行的,虽然不理想的解决方案。 - Kris B