警告:此問題中的XML錯誤,請在您迷惑自己之前閱讀答案!帶有九塊項目背景問題的ListView
我一直在敲打我的頭掛在牆上了一會兒。下面的帖子已經就這一課題予以澄清,但都沒能解決我的問題:Android ListView State List not showing default item background和ListView item background via custom selector
正確九宮背景顯示完全當我選擇列表中的項目,但我不能得到默認的九宮背景最初顯示。在我看來,我需要設置默認的項目背景,但我不知道如何去做。
列表查看:
<?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="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/shopListHeader"
/>
<ListView
android:id="@+id/shopList"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:dividerHeight="0px"
android:divider="#FFFFFFFF"
android:listSelector="@drawable/shop_list_selector"
/>
</LinearLayout>
選擇:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<!-- the list items are enabled and being pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/shop_list_item_pressed" />
<item
android:state_selected="true"
android:textColor="#FFFFFFFF" />
</selector>
背景:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true">
<item
android:state_selected="true"
android:drawable="@android:color/transparent" />
<item
android:drawable="@drawable/shop_list_item" />
</selector>
正如你所看到的,我已經簡單化了從引用的例子。
您可能還會注意到背景選擇器未被引用到任何地方。我開始在隨機的地方扔掉它(如果應用程序編譯的附加要麼沒有效果,要麼導致強制關閉)
我也試圖阻止文本的顏色從黑色和灰色變爲黑色和灰色時被選中但未被按下(可以通過滾動列表完成)。由於我的背景在中央爲黑色,因此選中時文字變得部分不可見。就我所知,該添加(Selector中的最後一個節點節點)什麼也不做。
有沒有人有任何想法得到這個荒謬的耗時的功能工作?
感謝您的建議,不幸的是,這並沒有解決問題:( – LostNomad311 2011-03-25 22:58:15
你是否使用這個作爲'ListView'內部項目佈局的背景? – raukodraug 2011-03-28 14:26:37