2010-01-12 70 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