有没有办法在 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/