Accessing resources programmatically

前端 未结 3 540
[愿得一人]
[愿得一人] 2020-12-10 07:36

Is it possible to receive the resource-ids being kept by a as an int[] programmatically without referring to the resource-class R?



        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-10 08:24

    Slightly more efficient solution:

    public static final int[] getResourceDeclareStyleableIntArray(String name) {
            Field[] allFields = R.styleable.class.getFields();
            for (Field field : allFields) {
                if (name.equals(field.getName())) {
                    try {
                        return (int[]) field.get(R.styleable.class);
                    } catch (IllegalAccessException ignore) {}
                }
            }
    
            return null;
        }
    

提交回复
热议问题