【Android】获取 id 数组资源

// strings.xml

    @drawable/user_guide_1
    @drawable/user_guide_2
    @drawable/user_guide_3
    @drawable/user_guide_4


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

你可能感兴趣的:(【Android】获取 id 数组资源)