时间:2022-08-30 09:30:01 | 栏目:Android代码 | 点击:次
动态取得R文件的int值,通过string-array name属性动态获取R.array.xxx的int值
private Object arrayResource = null;
private Object getArrayResource() {
if (arrayResource == null) {
Class<?> resource = R.class;
try {
Class<?>[] classes = resource.getClasses();
for (Class<?> c : classes) {
int i = c.getModifiers();
String className = c.getName();
String s = Modifier.toString(i);
if (s.contains("static") && className.contains("array")) {
return c.getConstructor().newInstance();
} else {
continue;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return arrayResource;
}