资源数组 [英] Resource arrays

查看:79
本文介绍了资源数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在XML中定义一个元素作为资源引用的数组? 例如(它不起作用,但是它解释了我想要的东西):

Is there a way to define an array in XML in which the elements are resource references? For example (it doesn't work, but it explains what I want):

<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()访问它们不起作用). (您可以在此处了解更多信息,这是我从中获得解决方法的来源.)

If I understand your question correctly, I think I know a workaround (since accessing them via getResources().getIntArray() doesn't work). (You can read more here, that is the source I took the workaround from.)

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]));

这篇关于资源数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆