问题描述
我有一个像这样的资源文件:
I have a resource file like this:
strings.xml
<string name="category01">My Category 01</string>
<string name="category02">My Category 02</string>
<string name="category03">My Category 03</string>
<string-array name="array_category_01">
<item name="title">@string/category01</item>
<item name="image">@drawable/img01</item>
</string-array>
<string-array name="array_category_02">
<item name="title">@string/category02</item>
<item name="image">@drawable/img02</item>
</string-array>
<string-array name="array_category_03">
<item name="title">@string/category03</item>
<item name="image">@drawable/img03</item>
</string-array>
<string-array name="categories_array">
<item>@array/array_category_01</item>
<item>@array/array_category_02</item>
<item>@array/array_category_03</item>
</string-array>
注意:@ drawable/img01,@ drawable/img02和@ drawable/img03是位于res \ drawable文件夹中的png图像
Note: @drawable/img01, @drawable/img02 and @drawable/img03 are png images locates at res\drawable folder
稍后,我将在下面的迭代中执行以检索类别-图像对:
Later I perform below iteration to retrieve category-images pairs:
Resources res = this.context.getResources();
TypedArray ta = res.obtainTypedArray(R.array.categories_array);
int n = ta.length();
String[][] array = new String[n][];
for (int i = 0; i < n; ++i) {
int id = ta.getResourceId(i, 0);
if (id > 0) {
array[i] = res.getStringArray(id);
} else {
// something wrong with the XML
}
}
ta.recycle();
例如,最后我得到一个数组,其内容为:
so for example at the end I get an array which contents are:
array[0]
|
---> [0] "My Category 01"
|
---> [1] "res/drawable-xxhdpi-v4/img01.png"
array[1]
|
---> [0] "My Category 02"
|
---> [1] "res/drawable-xxhdpi-v4/img02.png"
array[2]
|
---> [0] "My Category 03"
|
---> [1] "res/drawable-xxhdpi-v4/img03.png"
这正是我所期望的,直到这里都没问题.
It is just what I am expected to get, no problem until here.
稍后在我的代码中,我需要获取上述数组中包含的类别之一的资源ID,以便设置ImageView对象的内容,因此我在下面执行(假设我想要类别2的图像) :
Later in my code, I need to get the resource ID of one of the categories contained in the above array in order for set the content of an ImageView object so I perform below (let's say I want the image for category 2):
ImageView imageView = (ImageView) rowView.findViewById(R.id.categoryicon);
int resID = this.context.getResources().getIdentifier(array[1][1] , "drawable", this.context.getPackageName());
imageView.setImageResource(resID);
我的问题是resID为零,因此未找到可绘制资源...
My problem is that resID is zero so it means drawable resource has not been found...
为了工作,我需要数组中的图像名称为img01,img02,img03,而不是"res/drawable-xxhdpi-v4/img01.png","res/drawable-xxhdpi" -v4/img02.png"和"res/drawable-xxhdpi-v4/img03.png",因此,当我通过array [x] [y]将名称传递给getIdentifier时,它会起作用.
In order to work I need image names in the array to be img01, img02, img03 instead of "res/drawable-xxhdpi-v4/img01.png", "res/drawable-xxhdpi-v4/img02.png" and "res/drawable-xxhdpi-v4/img03.png" respectively so when I pass the names to getIdentifier through array[x][y] it works.
我该如何摆脱呢?
推荐答案
一个简单的解决方案是解析字符串并仅获取如下所示的图像名称.
One simple solution would be to parse the string and get only the image name as below.
String[] Image1 = array[1][1].split("/");
String s = Image1[Image1.length-1].replace(".png","").trim();
int resID = this.context.getResources().getIdentifier(s , "drawable", this.context.getPackageName());
这篇关于尝试从其名称在Android中获取可绘制资源ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!