问题描述
如何为每个选定的列表项执行此操作.
How do I do this per selected list item.
我尝试将其添加到 android:background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@color/android_green" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@color/black_alpha" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@color/black_alpha" />
<item android:drawable="@color/white_alpha" />
</selector>
但它不起作用,它改变了整个ListView.
but it does not work, it changes the entire ListView.
仍然无法正常工作,这是我目前所拥有的
Still not working, here is what I have so far
::listview_background
::listview_background
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@color/black_alpha" />
<item android:drawable="@color/white_alpha" />
</selector>
::我的观点是使用上面的
::My view that is using the above
<ListView android:id="@+id/list" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_below="@+id/round"
android:listSelector="@drawable/listview_background">
</ListView>
问题是 black_alpha 将应用整个列表,而不仅仅是选定的列表项
The problem is that the black_alpha will apply the the entire list, not just the selected list item
推荐答案
这里有一个 好文章,介绍如何将选择器与列表结合使用.
Here's a good article on how to use selectors with lists.
与其将其设置为ListView的android:background
,我相信您希望如下设置android:listSelector
:
Instead of setting it to be the android:background
of the ListView, I believe you want to set android:listSelector
as shown below:
<ListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:divider="@null"
android:dividerHeight="0dip"
android:listSelector="@drawable/list_selector" />
这篇关于将 ListView 中的选择从橙色更改为绿色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!