2010-02-02 18 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

相關問題