【Android】获取 id 数组资源

// strings.xml
<string-array name="user_guide_sequence">
    <item>@drawable/user_guide_1</item>
    <item>@drawable/user_guide_2</item>
    <item>@drawable/user_guide_3</item>
    <item>@drawable/user_guide_4</item>
</string-array>

// Java获取
public class IdArrayExtractor {
    public static int[] get(@ArrayRes int arrayId){
        TypedArray ar = App.getApp().getResources().obtainTypedArray(arrayId);
        int len = ar.length();
        final int[] resourceIds = new int[len];
        for (int i = 0; i < len; i++)
            resourceIds[i] = ar.getResourceId(i, 0);
        ar.recycle();
        return resourceIds;
    }
}

final int[] userGuideImages = IdArrayExtractor.get(R.array.user_guide_sequence);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容