2010-06-17 38 views
2

我需要設置在列表視圖中的行交替色彩,但是當我做,它消除/禁用上聚焦默認黃色背景具有交替顏色和聚焦色的Android的ListView

我的backgroundColor rowView.setBackgroundColor嘗試(一些顏色);

也與backgrounddrwable。

rowView.setBackgroundColor(R.drawable.view_odd_row_bg); 
<?xml version="1.0" encoding="utf-8"?> 
<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_window_focused="false" 
     android:drawable="@color/odd" /> 

    <!-- 
     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="@color/highlight" /> 
    <item 
     android:state_focused="true" 
     android:state_enabled="false" 
     android:drawable="@color/highlight" /> 

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

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

</selector> 

,但它不會工作。

有什麼辦法可以同時設置背景顏色和焦點顏色。

回答

7

我發現本教程對我非常有幫助。

http://ykyuen.wordpress.com/2010/03/15/android-%E2%80%93-applying-alternate-row-color-in-listview-with-simpleadapter/

我是能夠使ListView的項目已交替的顏色加關注項目保持默認的高亮顏色。

+1

我該怎麼做,使用bindView? 我試過這樣的事情: int i = 0; view.setBackgroundResource(i%2 == 0?R.drawable.bindview_bg_gray:R.drawable.bindview_bg_white); i ++; 但經過幾次滾動它變得雜亂無章! – 2010-07-16 04:44:33

+4

非常遲到的評論,但後代:菲利普是依靠bindView的順序調用,你不應該這樣做。永遠不要假設任何關於bindView調用的順序。而是嘗試對cursor.getPosition()(遊標是bindView方法的參數之一)進行模運算。 – benvd 2010-11-22 11:19:06

2

在您的顏色中添加一個透明度,黃色突出顯示仍然存在,它只是在背景顏色後面,透明度會讓它透出。