Android的ListView中选择颜色颜色、Android、ListView

由网友(旧人揪心)分享简介:大家好, 我有2个问题关于在Android中一个ListView:I have 2 questions regarding a ListView in Android:我怎么能获得 ListView的焦点行的颜色?我试图使用 ListView.getSelector()方式,根据其文档应该给我,我要找的,但它给...

大家好,

我有2个问题关于在Android中一个ListView:

I have 2 questions regarding a ListView in Android:

我怎么能获得 ListView的焦点行的颜色? 我试图使用 ListView.getSelector()方式,根据其文档应该给我,我要找的,但它给我,我不知道如何检索可绘制对象从颜色(如果可能的话...)。

How can I get the 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...).

我怎么能设置 ListView的焦点行的颜色? 在这里,我试图使用 setSelector()方式上的列表视图,传递一个ColorDrawable对象,但这样做的结果是,列表视图的整个背景是画在色彩...这不是我想要的东西,当然...

How can I set the 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...

谢谢!

推荐答案

该列表选择可绘制的是StateListDrawable —它包含引用到多个可绘制每个状态的列表可以,比如选择,有重点,pressed,残疾人......

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...

虽然你可以使用检索绘制 getSelector(),我不相信你可以检索特定绘制对象 StateListDrawable ,也不会似乎可以以编程方式直接获取颜色 Col​​orDrawable 反正。

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.

至于设置颜色,你需要一个 StateListDrawable 如上所述。您可以使用 Android的设置这个清单上:listSelector 属性,定义绘制在XML中是这样的:

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:

<?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>
阅读全文

相关推荐

最新文章