Android ListView 选择器颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2038040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android ListView Selector Color
提问by WhiteTigerK
Hi All,
大家好,
I have 2 questions regarding a ListView in Android:
我有两个关于 Android 中的 ListView 的问题:
How can I getthe color of the listview's focused row ? I tried to use the ListView.getSelector()method, which according to its documentation should give me what I'm looking for, but it's giving me a Drawable object which I don't know how to retrieve the color from (if possible...).
How can I setthe color of the listview's focused row ? Here I tried to use the setSelector()method on the listview, passing it a ColorDrawable object, but the result of doing it is that the whole background of the list view is painted in that color... and this is not what I wanted of course...
如何获取列表视图焦点行的颜色?我试图使用ListView.getSelector()方法,根据它的文档应该给我我正在寻找的东西,但它给了我一个 Drawable 对象,我不知道如何从中检索颜色(如果可能的话。 ...)。
如何设置列表视图焦点行的颜色?在这里,我尝试在列表视图上使用setSelector()方法,将一个 ColorDrawable 对象传递给它,但这样做的结果是列表视图的整个背景都以该颜色绘制......而这不是我想要的当然...
Thanks!
谢谢!
回答by Christopher Orr
The list selector drawable is a StateListDrawable
— it contains reference to multiple drawables for each state the list can be, like selected, focused, pressed, disabled...
列表选择器 drawable 是一个StateListDrawable
- 它包含对列表可以处于的每个状态的多个可绘制对象的引用,例如选中、聚焦、按下、禁用......
While you can retrieve the drawable using getSelector()
, I don't believe you can retrieve a specific Drawable
from a StateListDrawable
, nor does it seem possible to programmatically retrieve the colour directly from a ColorDrawable
anyway.
虽然您可以使用 检索可绘制对象getSelector()
,但我不相信您可以Drawable
从 a检索特定内容StateListDrawable
,而且似乎也不可能以编程方式直接从 a 检索颜色ColorDrawable
。
As for setting the colour, you need a StateListDrawable
as described above. You can set this on your list using the android:listSelector
attribute, defining the drawable in XML like this:
至于设置颜色,您需要StateListDrawable
如上所述。您可以使用android:listSelector
属性在列表中设置它,在 XML 中定义可绘制对象,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_focused="true"
android:drawable="@drawable/item_disabled" />
<item android:state_pressed="true"
android:drawable="@drawable/item_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/item_focused" />
</selector>
回答by Richard Le Mesurier
TO ADD: @Christopher's answerdoes not work on API 7/8 (as per @Jonny's correct comment) IF you are using colours, instead of drawables. (In my testing, using drawables as per Christopher works fine)
添加:@Christopher 的答案不适用于 API 7/8(根据 @Jonny 的正确评论),如果您使用的是颜色而不是可绘制对象。(在我的测试中,按照 Christopher 使用 drawable 工作正常)
Here is the FIX for 2.3 and belowwhen using colours:
这是使用颜色时2.3 及以下版本的FIX:
As per @Charles Harley, there is a bug in 2.3 and below where filling the list item with a colour causes the colour to flow out over the whole list. His fix is to define a shape
drawable containing the colour you want, and to use that instead of the colour.
根据@Charles Harley,在 2.3 及以下版本中存在一个错误,即用颜色填充列表项会导致颜色溢出整个列表。他的解决方法是定义一个shape
包含您想要的颜色的可绘制对象,并使用它而不是颜色。
I suggest looking at this link if you want to just use a colour as selector, and are targeting Android 2 (or at least allow for Android 2).
如果您只想使用颜色作为选择器,并且针对 Android 2(或至少允许 Android 2),我建议查看此链接。