今天想把图片也放到string.xml文件夹中便于读取使用,但是发现好像不行,多亏了这位兄台,直接上代码!!!
主要就是 TypeArray这个东西,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="feed_names">
<item>新闻</item>
<item>视频</item>
<item>国际新闻</item>
<item>体育</item>
<item>艺术</item>
<item>餐饮</item>
</string-array>
<string-array name="feed_icons">
<item>@drawable/latest</item>
<item>@drawable/video</item>
<item>@drawable/world</item>
<item>@drawable/sports</item>
<item>@drawable/arts</item>
<item>@drawable/dining</item>
</string-array>
</resources>
Map<String, Object> map;
TypedArray ta = getResources().obtainTypedArray(R.array.feed_icons);
String[] titleArr = getResources().getStringArray(R.array.feed_names);
for(int i=0; i<titleArr.length; i++)
{
map = new HashMap<String, Object>();
map.put("icon", ta.getResourceId(i, 0));
map.put("title", titleArr[i]);
list.add(map);
}
TypedArray ta = getResources().obtainTypedArray(R.array.feed_icons);
读取时:
int id = ta.getResourceId(index, 0);