2011-03-24 30 views
2

警告:此問題中的XML錯誤,請在您迷惑自己之前閱讀答案!帶有九塊項目背景問題的ListView


我一直在敲打我的頭掛在牆上了一會兒。下面的帖子已經就這一課題予以澄清,但都沒能解決我的問題:Android ListView State List not showing default item backgroundListView 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中的最後一個節點節點)什麼也不做。

有沒有人有任何想法得到這個荒謬的耗時的功能工作?

回答

5

我要刪除這個線程,但我不能,所以我會看到,如果我不能用這個作爲什麼不該做的例子:)

首先,在ListView XML中:android:listSelector="@drawable/shop_list_selector"
不要那樣做!

我在這裏想要做的是設置列表項的背景和android:background屬性不起作用。您可能已經注意到該項目XML丟失了,那是因爲它從我的頭上遺失了! (我從未觸及過無數個小時,我在這個'問題'上抨擊)所以行android:background="@drawable/shop_list_selector"進入該項目的屬性,一切都是groovy。 (記住上面的XML是非常錯誤的,所以不要使用它!)

...除了在現實生活中看起來不像在我的頭上那樣好:(
回到繪圖板!!!

3

您還沒有定義的「正常」狀態,看這個例子

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/blue" 
      android:state_pressed="true" /> 
    <item android:color="@color/red" 
      android:state_selected="true" /> 
    <item android:color="@color/white" /> 
</selector> 
在這裏白

是「正常」狀態,here你可以找到關於它的一些文檔。

我希望這有助於

+0

感謝您的建議,不幸的是,這並沒有解決問題:( – LostNomad311 2011-03-25 22:58:15

+0

你是否使用這個作爲'ListView'內部項目佈局的背景? – raukodraug 2011-03-28 14:26:37