2010-02-16 37 views
4
<?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="?android:attr/listPreferredItemHeight" 
    android:padding="6dip"> 
    <ImageView 
     android:id="@+id/icon" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:layout_marginRight="6dip" 
     android:src="@drawable/icon" /> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="0dip" 
     android:layout_weight="1" 
     android:layout_height="fill_parent"> 
     <TextView 
      android:id="@+id/toptext" 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
      android:textStyle="bold" 
     /> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:id="@+id/bottomtext" 
      android:singleLine="false" 
     /> 
    </LinearLayout> 
</LinearLayout> 

這是我目前的行。如果我創建了一個.JPEG,並且我希望它適用於每個項目......我將如何更改這個.xml文件?我會在哪裏放置圖像?在資產?如何設置背景圖片在Android的ListView中的每一個項目?

回答

3

如果你想爲每個列表項單獨的背景,您必須聲明自己的自定義適配器。

從BaseAdapter派生它,實現的最重要的部分是getView(int, View, ViewGroup)方法。

您必須瞭解在滾動列表時Android如何重新使用已存在的列表項視圖元素。這意味着:在任何時候,只會同時在屏幕上看到生成的視圖數量。

這種不會產生太多視圖的最佳策略會導致問題,您將不得不根據調用getView時currenty的位置爲每個列表項設置背景。如果您只是在生成視圖時嘗試靜態設置背景,它將再次重新出現,可能會附加到錯誤的元素。

getView方法要麼帶一個「convertView」作爲其第二個參數或不(null)。如果你的方法是通過convertView設置爲某個東西來調用的,那就意味着:「現在再重複使用這個視圖」。

這裏使用的技術在API演示(部分列表)中有很好的描述,還有一個視頻博客。

下面是它可能會如何做:

public class MyAdapter extends BaseAdapter { 

    public View getView(int position, View convertView, ViewGroup parent) { 
     // position is the element's id to use 
     // convertView is either null -> create a new view for this element! 
     //    or not null -> re-use this given view for element! 
     // parent is the listview all the elements are in 


     if (convertView == null) { 
      convertView = mInflater.inflate(R.layout.your_layout, null); 

      // here you must do whatever is needed to populate the elements of your 
      // list element layout 
      ... 
     } else { 
      // re-use the given convert view 

      // here you must set all the elements to the required values 
     } 

     // your drawable here for this element 
     convertView.setBackground(...); 

     // maybe here's more to do with the view 
     return convertView; 
    } 
} 

這基本上它。如果只有一些背景圖,我可能會緩存它們,因此您不必一遍又一遍讀取資源!

玩得開心!

+0

你如何緩存它們? – TIMEX

+0

在適配器中保存一個列表或數組(無論套件是否需要),並讀取適配器構造函數中的所有可繪製對象。然後,在設置背景時,不要在那裏加載資源,而是使用列表或數組元素! 但首先:不關心這一點。這只是我的一個附加提示,可以防止您的適配器在您的列表中亂拋垃圾! 祝你好運! – Zordid

+0

如何製作可繪製的repeat-x?在CSS中,我可以做repeat-x :) – TIMEX

0

你想有一個列表視圖,其中每一行都有自己的背景,像下面的一個?

alt text

在代碼中,你最有可能會設置視圖的ListAdapter。您可以構建的一些適配器採用佈局或查看參數。你應該只需要設置安卓背景屬性爲您指出這一點的看法。

例如,我可能有一個ArrayAdapter這樣創建:

new ArrayAdapter<String>(context, R.layout.item, R.id.itemName); 

當然,我用的ListViewsetAdapter與此適配器。在我item.xml文件,我可以定義文本視圖ITEMNAME這樣的:

<TextView android:id="@+id/itemName" android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:text="Test view" android:background="@drawable/add"/> 

不過,我不認爲ListAdapter的各類具有該功能的構造函數。檢查ListAdapter類型的您正在使用的文檔。如果你正在使用一個沒有此構造,我想你可能要繼承適配器和覆蓋適配器的getView從XML文件虛增您的看法。

我在每個res/drawable文件夾中都有圖像add.png。

相關問題