已經閱讀了一些相關的問題在這裏SO,以及通過Android文檔和源查看嘗試找出這一點,但我很難過,但考慮到listSelector似乎只對選定的項目適用的風格,我不會感到震驚......Android ListView狀態列表不顯示默認項目背景
我在這裏main.xml中定義的列表視圖:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fadingEdgeLength="5dp"
android:divider="#000000"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
<TextView
android:id="@android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:singleLine="false"
android:text="@string/message_list_empty" />
</FrameLayout>
引用的listSelector是在這裏(從很大程度上借用在SDK中找到默認的Android狀態列表:/android/platforms/android-8/data/res/drawable/list_selector_background.xml):
個<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- the window has lost focus, disable the items -->
<item
android:state_window_focused="false"
android:drawable="@drawable/shape_row_disabled" />
<!-- the list items are disabled -->
<item
android:state_enabled="false"
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/shape_row_disabled" />
<item
android:state_enabled="false"
android:state_focused="true"
android:drawable="@drawable/shape_row_disabled" />
<item
android:state_enabled="false"
android:drawable="@drawable/shape_row_disabled" />
<!-- the list items are enabled and being pressed -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/shape_row_transition" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/shape_row_transition" />
<!-- the list items are enabled and being used -->
<item
android:state_focused="true"
android:drawable="@drawable/shape_row_selected" />
<!-- the default item -->
<item
android:drawable="@drawable/shape_row" />
</selector>
引用的可繪是形狀,圓角矩形,像這樣:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="#EF6100"
android:centerColor="#FF8E00"
android:endColor="#EF6100"
android:angle="270" />
<corners
android:bottomRightRadius="7dp"
android:bottomLeftRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
所以,最終的結果應與通過默認用@繪製/ shape_row背景項一個ListView,然後不同取決於狀態的彩色背景。當我啓動我的列表時,一切狀態都處於活動狀態,比如項目獲得焦點,被按下等等,但是當沒有選擇任何項目(即默認視圖狀態)時,項目本身具有透明背景。我希望國家名單的最後一條規則,
<item android:drawable="@drawable/shape_row" />
會捕獲該狀態,但由於某種原因,它不起作用。我已經移動了一些東西並嘗試了不同的設置,而沒有任何設置如果我編輯用於在ListView中定義列表項的row.xml文件,並嘗試添加指向@ drawable/shape_row的特定android:background,每行都會獲得正確的背景,但是按下後,焦點上,等等狀態無法處理(或至少,不能被視爲背景永遠不會改變)。
任何人都可以點我在這裏向正確的方向?我接近於把這個睡覺,但嘗試幾乎所有的東西后,只是無法讓ListView項目採取默認背景和通過android:listSelector響應實施狀態列表。
感謝,
保羅
編輯:
還只是嘗試添加機器人:itemBackground = 「@繪製/ shape_row」 到main.xml中ListView控件,並不出所料沒有運氣(該文檔狀態它應該用來指定菜單項目的背景,但認爲它值得拍攝列表項目)。
另一件事情嘗試了,爲每個選擇器和項目定義添加了android:visible =「true」。 StateListDrawable文檔似乎表明"Provides initial visibility state of the drawable; the default value is false",但添加此變化沒有改變。編輯2:所以,基於以下由Qberticus所做的研究,似乎每個視圖只能有一組列表選擇器,這確認了列表選擇器的行爲。我也可以確認設置android:drawSelectorOnTop確實移動了所選項目的選擇器IN FRONT,但是這當然掩蓋了項目本身,因此我只能看到選擇器本身(在我的例子中是一個彩色形狀)。
這裏是清單的樣子設置背景:
由於設置的背景,在外觀上沒有任何變化時選定的項目。如果我改變了android:drawSelectorOnTop指令爲true,選擇一個項目,我得到:
最後,如果我設置列表項沒有背景圖片,選擇正常工作,但當然,有沒有背景圖片爲StateSelector的最後一條規則似乎並沒有遵守(它不應該作爲一個包羅萬象的?)對非選擇的項目意味着沒有好的自定義圓角形狀:
所以,我的問題依然存在;如果我沒有爲每個列表項目設置背景,我可以看到選擇器正在工作,但未選中項目時沒有背景。如果我爲這個項目設置了一個背景,當它沒有被選中時它們看起來很棒,但是背景遮擋了選擇器。使用工作選擇器是否無法使用唯一形狀的列表項?
感謝誰比誰的輕量化。
感謝您的辛勤工作!當你提到listSelector只用於每個ListView時,這就是我在ListView XML中指定的。設置每個單獨的List項目(他們是TextViews)有一個背景意味着listSelector被隱藏......沒有背景意味着每個TextView的透明背景,直到選擇器啓動。我嘗試使用ListView的drawSelectorOnTop指令,因爲我可以在背景中看到選擇器,但它也在前景之上,也掩蓋了TextView的內容。 – 2011-01-19 18:24:09
也許這只是不可能有一個自定義(在我的情況下,四捨五入)ListView項目和適當的選擇器行爲......也許我需要指定整個ListView的背景,說一種顏色,然後選擇器將正確顯示。希望有圓角的ListView項目和選擇器交互,但也許這是不可能的。 – 2011-01-19 18:26:05