列出具有交替颜色的项目
我有一个列表视图和一个适配器,用于为列表项设置交替背景颜色(“斑马”列表样式):
public View getView(final int position, View convertView, ViewGroup parent) {
int colorPos = position % colors.length;
...
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}
但是现在,当我使用滚轮选择一个项目时,或者当我单击一个项目时,用于选择/的原始颜色单击“不覆盖我的自定义背景”(我可以在我设置的背景下方看到原始颜色)。
如何设置这些状态的原始颜色?
I have a list view and an adapter that sets alternating background colors to the list items ("zebra" list style):
public View getView(final int position, View convertView, ViewGroup parent) {
int colorPos = position % colors.length;
...
convertView.setBackgroundColor(colors[colorPos]);
return convertView;
}
But now, when i select an item using scroll wheel, or when I click an item, the original colors for selecting/clicking do not override my custom backgrounds (I can see the original color below the one I set).
How can I set the original colors for these states?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为最简单的方法是创建两个选择器用作背景资源,在state_selected模式下使用透明颜色:
(res/drawable/alterselector1.xml:)
(res/drawable/alterselector2.xml:)
(res/values/colors.xml:)
然后使用 setBackgroundResource 方法在适配器的 getView 方法中设置背景:
现在,当您选择一行,您的背景不会将原始选择器隐藏在后面。
I think the easiest way is to create two selectors which are used as the background resources, with transparent color in the state_selected mode:
(res/drawable/alterselector1.xml:)
(res/drawable/alterselector2.xml:)
(res/values/colors.xml:)
Then you set the backgrounds in the getView method of the adapter with the setBackgroundResource method:
Now when you select a row, your background don't hide the original selector behind.
您需要更改列表突出显示颜色
如果您通过样式执行此操作
,或者可以在代码中设置相同的属性
my_selector 是一个状态可绘制对象 - 在 SDK 目录中查找示例:
You need to change your list highlight color
if you do it via styles
or you can set same attribute in code
my_selector is a state drawable - look for examples in the SDK directory :