将 R.drawable ID 存储在 XML 数组中 [英] Storing R.drawable IDs in XML array
本文介绍了将 R.drawable ID 存储在 XML 数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用 XML 值文件以 R.drawable.*
的形式将可绘制资源的 ID 存储在数组中,然后在我的活动中检索该数组.
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?
推荐答案
您使用 /res/values 文件夹中的 arrays.xml
文件中的 ="noreferrer">类型化数组,如下所示:
You use a typed array in arrays.xml
file within your /res/values
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, use 0 as default to set null resource
imgs.getResourceId(i, 0)
// or set you ImageView's resource to the id
mImgView1.setImageResource(imgs.getResourceId(i, 0));
// recycle the array
imgs.recycle();
这篇关于将 R.drawable ID 存储在 XML 数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文