【发布时间】:2015-08-12 00:22:15
【问题描述】:
我们可以从Typedarray中获取资源id,它包含android中xml文件中的字符串吗?
【问题讨论】:
-
你的问题太模糊了。你到底想要什么?你的 TypedArray 是什么样子的?
-
检查我编辑的答案
标签: android typedarray
我们可以从Typedarray中获取资源id,它包含android中xml文件中的字符串吗?
【问题讨论】:
标签: android typedarray
<string-array name="array01">
<item name="id">1</item>
<item name="title">item one</item>
</string-array>
<!-- etc. -->
<array name="array0">
<item>@array/array01</item>
<!-- etc. -->
</array>
然后在你的代码中你做这样的事情:
Resources res = getResources();
TypedArray ta = res.obtainTypedArray(R.array.array0);
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(); // Important!
【讨论】: