24

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

1 回答 1

6

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

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]));
于 2012-05-27T15:17:37.473 回答