Android上的ListView项目更改背景颜色 [英] Changing background color of ListView items on Android
问题描述
我怎样才能改变的ListView
项目的背景色在每个项目的基础。当我使用安卓的backgroundColor
在的ListView
项目布局,我可以做到这一点,但是,该列表选择不再可见。我可以通过设置使选择再次可见 drawSelectorOnTop
为真,但随后选择覆盖了整个项目。
How can I change background color of ListView
items on a per-item basis. When I use android:backgroundColor
in the ListView
item layout I can achieve this, however the list selector is no longer visible. I can make the selector visible again by setting drawSelectorOnTop
to true but then the selector overlays the whole item.
任何想法如何改变这些背景颜色和保持选择?
Any ideas how to change those background colors and keep the selector?
PS我宁愿不改变选择本身。
PS I would rather not change the selector itself.
编辑:Gmail应用程序的作者已成功地实现这个所说所以这是绝对有可能。
Authors of GMail application have managed to achieve exactly this so it's definitely possible.
推荐答案
您必须创建一个不同的状态绘制为要使用的每种颜色。
You have to create a different state drawable for each color you want to use.
例如: list_selector_read.xml
和 list_selector_unread.xml
。
所有你需要做的是把一切都透明,除了安卓state_window_focused =假
项。
All you need to do is set everything to transparent except the android:state_window_focused="false"
item.
然后,当你绘制你的列表,你叫 setBackgroundResource(R.drawable.list_selector_unread /读取)
的每一行。
Then when you are drawing your list you call setBackgroundResource(R.drawable.list_selector_unread/read)
for each row.
您没有设置在所有的ListView的一个listSelector。这将保持默认选择适合您的特定的Android的味道。
You don't set a listSelector on the ListView at all. That will maintain the default selector for your particular flavor of Android.
这篇关于Android上的ListView项目更改背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!