2010-01-12 149 views
7

我有一个列表视图和组交替的背景颜色列表项的适配器(“斑马线”列表样式)列表项轮子,或者当我点击一个项目时,选择/点击的原始颜色不会覆盖我的自定义背景(我可以在我设置的颜色下面看到原始颜色)。与交替的颜色

如何设置这些状态的原始颜色?

回答

20

我认为最简单的方法是创建它们在state_selected模式用作背景资源,具有透明颜色两个选择: (RES /抽拉/ alterselector1.xm升:)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_selected="false" 
     android:drawable="@drawable/altercolor1"/> 

</selector> 

(RES /绘制/ alterselector2.xml :)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_selected="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/transparent" /> 
    <item android:state_selected="false" 
     android:drawable="@drawable/altercolor2"/> 
</selector> 

(RES /价值/ colors.xml :)

<resources> 
    <drawable name="transparent">#00ffffff</drawable> 
    <drawable name="altercolor1">#ffffffff</drawable> 
    <drawable name="altercolor2">#ff000000</drawable> 
</resources> 

然后你设置的背景带有setBackgroundResource方法的适配器的getView方法:

if (position % 2 == 0){ 
    reusableView.setBackgroundResource(R.drawable.alterselector1); 
} else { 
    reusableView.setBackgroundResource(R.drawable.alterselector2); 
} 

现在,当您选择一行时,您的背景不会隐藏原始选择器。

+0

这部分工作 - 当使用滚动按钮聚焦项目时,我可以看到突出显示,但当按下项目时我无法使其工作。我尝试了所有列在这里的状态:http://developer.android.com/guide/topics/resources/color-list-resource.html,但没有任何工作... – zorglub76 2010-11-28 13:40:42

+1

我编辑了选择器来处理按下状态。似乎当你按下该项目时,它会失去它的选择状态。所以你必须将按下的状态定义为透明。只需要注意排序,因为选择器将使用匹配当前状态的第一个项目,所以state_selected =“false”项应该在最下面。 – Utyi 2010-11-29 10:57:11

+0

作品!自从我问这个问题差不多一年了!谢谢! – zorglub76 2010-11-30 10:37:04

2

你需要的,如果你通过风格

<style name="Widget.AbsListView"> 
     <item name="android:listSelector">@drawable/my_selector</item> 
</style> 

,或者你可以在代码中设置相同的属性做更改列表高亮颜色 my_selector是国家绘制 - 寻找在SDK目录的例子:

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2008 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:state_window_focused="false" 
     android:drawable="@color/transparent" /> 

    <!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. --> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_disabled" /> 
    <item android:state_focused="true" android:state_enabled="false" 
     android:drawable="@drawable/list_selector_background_disabled" /> 

    <item android:state_focused="true" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_transition" /> 
    <item android:state_focused="false" android:state_pressed="true" 
     android:drawable="@drawable/list_selector_background_transition" /> 

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

</selector> 
+0

我试过了,但无论我最初为背景设置了什么,通过在代码中创建“斑马纹”后被重写...是否有另一种方法来创建斑马纹外观? – zorglub76 2010-01-13 08:41:22