【问题标题】:Typed array for string which will give resource Id for android字符串的类型化数组,它将为 android 提供资源 ID
【发布时间】:2015-08-12 00:22:15
【问题描述】:

我们可以从Typedarray中获取资源id,它包含android中xml文件中的字符串吗?

【问题讨论】:

  • 你的问题太模糊了。你到底想要什么?你的 TypedArray 是什么样子的?
  • 检查我编辑的答案

标签: android typedarray


【解决方案1】:
<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!

【讨论】:

  • 我想要字符串而不是可绘制资源。请帮忙
猜你喜欢
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2015-04-10
  • 2011-08-20
  • 2011-05-24
  • 1970-01-01
  • 2018-03-23
相关资源
最近更新 更多