2010-02-02 79 views
45

我有一个列表选择器,像这样一个简单的列表。ListSelector适用于整个列表

<ListView android:id="@+id/list" android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:layout_below="@+id/round" 
    android:listSelector="#99000000" android:clickable="true" android:cacheColorHint="#00000000" android:background="#00000000"> 
</ListView> 

正如你所看到的android:listSelector =“#99000000”,但“黑阿尔法”颜色应用于整个列表,而不是选定的项目。


因此,这是我现在有,但整个列表中仍然变黑

:: listview_background.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/list_normal" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/list_pressed" /> 
    <item android:state_focused="true" 
     android:drawable="@drawable/list_active" /> 
</selector> 

:: colors.xml

<resources> 
    <drawable name="list_normal">#96FFFFFF</drawable> 
    <drawable name="list_active">#66000000</drawable> 
    <drawable name="list_pressed">#CA000000</drawable> 
</resources> 

::我的清单中的xml标记

android:listSelector="@drawable/listview_background" 
+0

我把文件选择在哪个文件夹? – Breedly 2013-10-04 02:14:15

+2

HoneyComb之前的所有版本中都有一个错误,它将列表选择器** color **应用于整个列表背景。 [查看我对另一个问题的回答](http://stackoverflow.com/a/15873704/383414)了解更多细节。这不是使用图像作为背景的问题,因此下面提供了所有解决方法。 – 2014-07-01 09:57:13

回答

28

我有同样的问题。我有一个自定义的背景图片,我不想让该背景图片的变体,因为这将是乏味的代表所有不同的状态。

所以我想做一件明显的事情,在关注的listitem顶部叠加一个半透明条,当用户点击“enter”键或其他任何东西时,它会闪烁到按下的叠加颜色,即更醒目,更不透明。

解决方案是远离任何引用listSelector内部颜色的@color或@drawable。我创建了两个3x3像素.png文件。每个都与伽玛层一起保存。在我的情况下,它是两种相同的颜色,每种颜色在Gimp中混合,并且在颜色层上具有不同的透明度。所以当你选择一个项目时,你会得到一个覆盖25%的颜色,当你按下它时,你会得到一个50%颜色的PNG。我把它们放在我可绘制为bg_list_item_pressed.png和bg_list_item_highlighted.png

然后我就按照列表中选择设置为:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <!-- Selected --> 
    <item 
    android:state_focused="true" 
    android:state_pressed="false" 
    android:drawable="@drawable/bg_list_item_highlighted" /> <!-- @drawable/tab_focus --> 

    <!-- Pressed --> 
    <item 
    android:state_pressed="true" 
    android:drawable="@drawable/bg_list_item_pressed" /> <!-- @drawable/tab_press --> 

</selector> 

然后我说我listSelector属性我的ListView在我的布局XML:

android:listSelector="@drawable/list_selector" 
android:drawSelectorOnTop="true" 

现在它的工作原理就是如何让它工作。包括使用D-pad选择一行,并用回车单击它。获得突出显示和后续按下的颜色应该如何。

+5

问题依然存在:为什么我们不能使用ColorDrawable进行单一状态?创建一个3像素的图像来表示一种颜色只是一种解决方法,而不是一个合适的解决方案。 – Matthias 2010-06-16 09:01:11

+0

谢谢!这个伎俩让你省了很多麻烦! – 2012-02-21 10:59:33

+0

非常感谢!它的工作就像魅力! – ranjith 2015-01-07 07:16:18

2

列表选择器是一个StateListDrawable —它包含引用到多个可绘制每个状态的列表可以像选择,有重点,按下,残疾人...

在你的情况下,通过设置单一颜色,该列表为每个单一状态绘制#9000。

如上所述,您需要定义StateListDrawable,其XML类似于以下内容。这是您在android:listSelector属性中设置的内容。

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

或者,要为所有列表相同的列表中选择您的应用程序,你可以在Widget.ListView主题覆盖的选择:

<style name="Widget.ListView" parent="Widget.AbsListView"> 
    <item name="android:listSelector">@drawable/my_custom_selector</item> 
</style> 
+0

嗨仍然不工作,请参阅上面的 – jax 2010-02-03 15:09:11

2

贾克斯,

我试图找出为什么我的整个名单是改变颜色设置listSelector,并发现这正是它应该做的,也就是我们设置颜色为整个列表,而不是我们想要的行。

下面是你想要做的: 我假设你有一个用XML定义的ListView。对于这个ListView,我假设你正在使用某种类型的适配器来设置数据。此外,对于此适配器,您正在使用一些在XML中定义的行视图。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:background="@drawable/textview_selector"> 
     <TextView  android:id="@+id/textForList" 
     android:layout_height="fill_parent" 
     android:layout_width="wrap_content" 
     android:padding="10sp" /> 
</LinearLayout> 

这里存储在绘制我textview_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector 
xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/teal" /> 
    <item android:drawable="@drawable/white" /> 
</selector> 

希望这有助于

+1

这很好,但是我发现它并不像我预期的那样工作,因为适配器视图不确定下一个关注哪个视图(大概只有适配器知道)。当然,股票ListView如何做它必须记录在某处,因为它比我的代码快得多,所以我想用这 – 2011-02-03 00:12:46

+2

这是明显错误的,AFAIK – manmal 2011-10-16 21:24:31

88

我有你想要做的是这样设置该行元素的背景是什么同样的问题,并且在查看平台可绘制XML文件之一时,我注意到通过创建XML形状来消除创建仅用于颜色的图像文件的需要。

例如,代替:

<item android:state_focused="true" 
     android:drawable="@drawable/list_active" /> 

务必:

<item android:state_focused="true"> 
    <shape> 
     <solid android:color="#66000000" /> 
    </shape> 
</item> 

除了只是创建一个素色抽拉,形状是柔性的,类似于简单矢量对象。所有的细节可以在这里找到:http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

+3

YES!谢谢你,我看到的唯一答案实际上解决了问题,并且没有解决问题。 – 2012-04-17 16:59:39

+1

为什么这不是一个解决方法?它仍然避免直接使用纯色资源,这是其他解决方法所解决的问题。 – JulianSymes 2013-09-16 11:26:17

+2

我不知道如何使用十六进制颜色值将不同问题的解决方案变为解决方法,但正如链接文档中所述,您还可以使用颜色资源。 '' – 2013-09-17 14:37:13

17

如果你正在为列表中的每个项目设置一个行布局,这就是我的做法。请注意设置为listSelector的透明值。 (为了简便起见,我除去colors.xml。)

main.xml中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView 
    android:id="@android:id/list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:listSelector="#00000000"/> 
</LinearLayout> 

row.xml

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/row" 
    android:layout_width="fill_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:background="@drawable/list_selector"> 
    ... 
</FrameLayout> 

list_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="#66FFFFFF" /> 
    <item android:drawable="#FF666666"/> 
</selector> 
+2

这应该是被接受的答案,因为它不依赖于某些“解决方法像素”。 – 2012-04-12 14:24:58

+0

你知道吗Sven Jacobs! – worked 2014-01-11 01:01:00

+0

谢谢,这真的有效 – Kibi 2014-06-10 13:53:35

1

你并不总是需要创建一个图片,你可以定义一个形状

<?xml version="1.0" encoding="utf-8"?> 

<item android:state_focused="true" android:state_pressed="false"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#40000000"/> 
    </shape> 
</item> 
<item android:state_pressed="true"> 
    <shape android:shape="rectangle"> 
     <solid android:color="#80000000"/> 
    </shape> 
</item> 

+0

我认为这比Eric的答案好得多。这样你就不会用无用的资源混淆你的项目。 – MinceMan 2013-07-19 20:20:58