2014-11-05 17 views
1

在Android的arrays.xml,我可以定義數組像下面,如何讓機器人arrays.xml陣列accroding值

<string-array name="feed_icons"> 

    <item value="0">@drawable/latest</item> 

    <item value="1">@drawable/video</item> 

    <item value="2">@drawable/world</item> 

    <item value="3">@drawable/sports</item> 

    <item value="4">@drawable/arts</item> 

    <item value="5">@drawable/dining</item> 

</string-array> 

我不知道如何獲得根據繪製的價值?有三個功能嗎?我只是想知道,

String[] iconArrays = getResource().getStringArrays(R.arrays.feed_icons); 

但它不是我想要的。

+0

下一次您的問題張貼在正確的網站上。 – slhck 2014-11-05 11:51:03

+0

你已經給出了0到n的值,所以你可以從String []中獲取合適的可繪製的位置,這是間接值。 – 2014-11-05 11:58:20

回答

2

你可以像一個數組保存繪製名稱:

<string-array name="feed_icons"> 
    <item value="0">latest</item> 
    <item value="1">video</item> 
</string-array> 

然後取繪製:

String[] iconArrays = getResources().getStringArray(R.arrays.feed_icons); 

// Get specific drawable resource id 
int drawableResId = this.getResources().getIdentifier(iconArrays[0], "drawable", getPackageName()); 

// Fetch drawable by resource id 
Drawable yourDrawable = getResources().getDrawable(drawableResId); 

// Use drawable 
imageView.setDrawable(yourDrawable); 
+0

它的工作原理,謝謝。 – gladman 2014-11-06 02:16:14