在XML数组中存储R.drawable的ID

158

我想将可绘制资源的ID以R.drawable.*的形式存储在一个XML值文件中的数组中,然后在我的活动中检索该数组。

有什么好的方法实现这个功能吗?


1
你能澄清一下你所说的“在XML中使用数组内部”的意思吗? - Eugene S
一个值文件,例如strings.xml。 - gammaraptor
1
我不明白为什么你想这样做。你能提供更多的背景信息,解释一下为什么你想以这种方式去做吗? - mattr-
听起来你正在尝试做比必要更复杂的事情。 - Octavian Helm
4
我想要做的是存储将在列表视图中显示的图片的ID。 - gammaraptor
5个回答

383

你在/res/values文件夹下使用了一个名为arrays.xml的文件,并且在文件中使用了一个类型数组,该数组看起来像这样:

<?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>

然后在您的Activity中,可以这样访问它们:

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();

6
你能解释一下 imgs.getResourceId(i,-1) 中的 -1 代表什么意思吗? -1 表示如果没有找到对应资源,将会返回该值作为默认值。 - Nishad
6
建议:在用户“imgs”添加以下行后,写入imgs.recycle()。 - benoffi7
9
默认id应该使用0而不是-1。-1是有效的资源id,而0是空资源。 - Alex
6
太棒了!类型化数组很不错。 - asgs
3
使用 length() 函数来获取 TypedArray 的元素数量。 - Roger Huang
显示剩余8条评论

33
value文件夹中创建一个xml文件,将其命名为arrays.xml,并按以下方式向其中添加数据。
<integer-array name="your_array_name">
    <item>@drawable/1</item>
    <item>@drawable/2</item>
    <item>@drawable/3</item>
    <item>@drawable/4</item>
</integer-array>

然后用以下方式将其添加到您的代码中

private TypedArray img;
img = getResources().obtainTypedArray(R.array.your_array_name);

然后,要在 TypedArray中使用这些的Drawable,例如作为ImageView的背景,请使用以下代码:

Resources res = getResources(); Drawable drawable = res.getDrawable(R.drawable.my_drawable); ImageView imageView = findViewById(R.id.my_image_view); imageView.setBackground(drawable);
ImageView.setBackgroundResource(img.getResourceId(index, defaultValue));

其中indexDrawable索引。defaultValue是当该index处没有条目时给出的值。

有关TypedArray的更多信息,请访问此链接:http://developer.android.com/reference/android/content/res/TypedArray.html


17

您可以使用此功能创建其他资源的数组,例如可绘制对象。请注意,该数组不需要是同质的,因此您可以创建混合资源类型的数组,但必须知道数组中数据类型的内容和位置。

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="icons">
        <item>@drawable/home</item>
        <item>@drawable/settings</item>
        <item>@drawable/logout</item>
    </array>
    <array name="colors">
        <item>#FFFF0000</item>
        <item>#FF00FF00</item>
        <item>#FF0000FF</item>
    </array>
</resources>

像这样在您的活动中获取资源

Resources res = getResources();
TypedArray icons = res.obtainTypedArray(R.array.icons);
Drawable drawable = icons.getDrawable(0);

TypedArray colors = res.obtainTypedArray(R.array.colors);
int color = colors.getColor(0,0);

享受吧!!!!!


5

在 Kotlin 中,你可以这样做:

 <integer-array name="drawer_icons">
    <item>@drawable/drawer_home</item>
</integer-array>
您将从资源中获取图像数组,类型为TypedArray
 val imageArray = resources.obtainTypedArray(R.array.drawer_icons)

通过索引获取资源 ID

imageArray.getResourceId(imageArray.getIndex(0),-1)

或者您可以将imageView的资源设置为id

imageView.setImageResource(imageArray.getResourceId(imageArray.getIndex(0),-1))

最后一次循环中,重新利用该数组

imageArray.recycle()

2

kotlin way could be this:

fun Int.resDrawableArray(context: Context, index: Int, block: (drawableResId: Int) -> Unit) {
  val array = context.resources.obtainTypedArray(this)
  block(array.getResourceId(index, -1))
  array.recycle()
}

R.array.random_imgs.resDrawableArray(context, 0) {
  mImgView1.setImageResource(it)
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接