android - 资源数组

标签 android string android-xml android-resources

有没有办法在 XML 中定义一个数组,其中元素是资源引用? 例如(它不起作用,但它解释了我想要什么):

<integer-array name="actions_images">
    <item>@drawable/pencil</item>
    <item>@drawable/pencil</item>
    <item>@drawable/pencil</item>
    <item>@drawable/pencil</item>
    <item>@drawable/pencil</item>
    <item>@drawable/pencil</item>
</integer-array>

最佳答案

如果我正确理解您的问题,我想我知道一种解决方法(因为通过 getResources().getIntArray() 访问它们不起作用)。 (您可以阅读更多 here ,这是我从中获取解决方法的来源。)

TypedArray ar = context.getResources().obtainTypedArray(R.array.my_array);
int len = ar.length();     
int[] resIds = new int[len];     
for (int i = 0; i < len; i++)     
    resIds[i] = ar.getResourceId(i, 0);

ar.recycle();                       
// Do stuff with resolved reference array, resIds[]...     
for (int i = 0; i < len; i++)     
    Log.v (TAG, "Res Id " + i + " is " + Integer.toHexString(resIds[i]));

关于android - 资源数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10774668/

相关文章:

android - 如何从类中获取字符串? (Android studio) abbyy OCR 识别

c - 使用 strcpy 和指向字符的指针

arrays - 如何将字符串转换为字符串数组

C#:将字符串插入另一个字符串 - 性能问题

自定义对话框的Android昏暗背景

android - 在 Android 中使用 ASyncTask 加载布局

java - Android listview左右对齐

android - 如何将抽屉导航添加到我现有的代码中?

java - 在 fragment 中绘制多层矩形

android - 使用 RecyclerView 制作基于客户列表适配器 Android 的水平 ListView