存储XML阵列R.drawable标识 [英] Storing R.drawable IDs in XML array
本文介绍了存储XML阵列R.drawable标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想用来存储绘制资源ID在 R.drawable的形式。*
使用XML值的文件,然后检索数组的数组里面我活动。
I would like to store drawable resources' ID in the form of R.drawable.*
inside an array using an XML values file, and then retrieve the array in my activity.
如何实现这一目标的任何想法?
Any ideas of how to achieve this?
推荐答案
您使用在 arrays.xml
文件类型数组你的中,看起来像这样/ RES
文件夹:
You use a typed array in arrays.xml
file within your /res
folder that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="random_imgs">
<item>@drawable/car_01</item>
<item>@drawable/balloon_random_02</item>
<item>@drawable/dog_03</item>
</integer-array>
</resources>
然后在你的活动,访问它们像这样:
Then in your activity, access them like so:
TypedArray imgs = getResources().obtainTypedArray(R.array.random_imgs);
// get resource ID by index
imgs.getResourceId(i, -1)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, -1));
// recycle the array
imgs.recycle();
这篇关于存储XML阵列R.drawable标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文