2010-05-10 26 views

回答

0

你想每一行正確的使用不同的圖標?那麼你可以使用所有圖片的資源ID整數數組,並把它們放在你的行項目中。人們這樣做的典型方式是硬編碼列表並指定資源中的R.drawable.iconName。像:

int[] mapPic = { 
     R.drawable.icon1,R.drawable.icon2, 
     R.drawable.icon3,R.drawable.icon4}; 

對我來說這是一個粗略的解決方案,硬編碼是一個很大的禁忌。更好的方法是將數組放入xml文件(如string.xml)或爲所有數組創建的文件。在你string.xml投入:

<array name="arrayIcon"> 
    <item>@drawable/icon1</item> 
    <item>@drawable/icon2</item> 
    <item>@drawable/icon3</item> 
    <item>@drawable/icon4</item> 
</array> 

然後在你的代碼,當你第一次創建的設置內容之前,你的觀點,在添加:

TypedArray mapPic = getResources().obtainTypedArray(R.array.arrayIcon); 

您可以訪問這實際上是該對象通過繪製:

Drawable drawable = typeArray.getDrawable(RowID); 

在我的代碼來設置它在我看來,我不得不搶ViewWrapper和使用調用getIcon()來設置它。

ViewWrapper wrapper.getIcon().setImageDrawable(drawable); 

這就是如何通過使用xml資源中的數組來添加圖像資源到列表中。希望這可以幫助。