我想使用XML值文件在数组中以R.drawable.*
的形式存储可绘制资源的ID,然后在我的活动中检索数组。
有关如何实现这一目标的任何想法?
答案 0 :(得分:342)
您在arrays.xml
文件夹中的/res/values
文件中使用typed array,如下所示:
<?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>
然后在您的活动中,像这样访问它们:
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();
答案 1 :(得分:30)
在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);
然后在Drawable
img
中使用TypedArray
这些ImageView
作为background
ImageView.setBackgroundResource(img.getResourceId(index, defaultValue));
使用以下代码
index
其中Drawable
是defaultValue
索引。
如果此index
TypedArray
是您给出的值
有关<div id ="bck" style="width:700px;height:500px">
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg">
<defs>
<pattern id="smallGrid" width="8" height="8" patternUnits="userSpaceOnUse">
<path d="M 8 0 L 0 0 0 8" fill="none" stroke="lightblue" stroke-width="0.5"/>
</pattern>
<pattern id="grid" width="80" height="80" patternUnits="userSpaceOnUse">
<rect width="80" height="80" fill="url(#smallGrid)"/>
<path d="M 80 0 L 0 0 0 80" fill="none" stroke="lightblue" stroke-width="0.5"/>
</pattern>
</defs>
<rect width="100%" height="100%" fill="url(#grid)" />
</svg>
</div>
的详细信息,请访问此链接
http://developer.android.com/reference/android/content/res/TypedArray.html
答案 2 :(得分:14)
您可以使用它来创建其他资源的数组,例如drawables。请注意,数组不需要是同类的,因此您可以创建混合资源类型的数组,但是您必须知道数组中数据类型的位置和位置。
<?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);
享受!!!!!
答案 3 :(得分:1)
在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()
答案 4 :(得分:0)
kotlin的方式可能是这样:
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)
}
答案 5 :(得分:-2)
据我所知,您无法在R.drawable中存储数组。
您可以做的是在config.xml或strings.xml中创建一个数组,使用alias resource将路径映射到可绘制资源。
看看是否有效,如果您需要任何其他帮助,请告诉我。