2011-08-16 64 views
11

我有一個列表視圖,當我觸摸它們時,項目上顯示一個黃色。所有我做不同的是改變在列表視圖XML的背景圖片,而現在它不再會告訴我的yellowtintAndroid的列表視圖不再突出顯示選擇onclick

這裏是代碼

列表視圖XML,它只是一個帶有背景的TextView圖片:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="24sp" 
    android:textColor="#000000" 
    android:background="@drawable/bglistitem" 
    android:gravity="center_vertical|center_horizontal"> 
</TextView> 

在另一個佈局,其中它被稱爲

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:padding="10dp" 
    android:textSize="24sp" 
    android:textColor="#000000" 
    android:background="@drawable/bglistitem" 
    android:gravity="center_vertical|center_horizontal"> 
</TextView> 

,這裏的地方是代碼:

lv.setOnItemClickListener(new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {} 
}); 

正如您在上面所看到的,我從未做過任何會改變選擇突出顯示的默認行爲的任何事情,爲什麼現在會有所不同,也許您看到我看不到?

回答

20

當您向ListView添加新背景時,您將覆蓋最有可能使用選擇器根據其狀態給ListItems着色的android默認背景。

嘗試使用自定義選擇

創建一個XML文件,mycustombackground.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/item_pressed" /> 
    <item android:state_focused="true" 
     android:drawable="@drawable/item_focused" /> 
    <item android:drawable="@drawable/item_normal" /> 
</selector> 

有關國家用自己更換@drawables。 然後將你的列表視圖的背景設置爲xml文件。

android:background="@drawable/mycustombackground" 

您可能想看看XML來創建黃色,或者只是創建自己的圖像。

+0

我想通了,謝謝你的洞察力,幫助我縮小了可能性。如果圖像中有任何alpha,則會發生黃色突出顯示。如果你有一個不透明的圖像,那麼它不會突出顯示 – CQM

相關問題