java反射android的r文件的示例
动态取得r文件的int值,通过string-array name属性动态获取r.array.xxx的int值
//通过string-array name属性动态获取r.array.xxx的int值
public charsequence[] getlistdata(string name) {
try {
class<?> arrayclass = getarrayresource().getclass();
field intfield = arrayclass.getfield(name);
int sourceid = intfield.getint(name);
return getresources().gettextarray(sourceid);
} catch (exception e) {
e.printstacktrace();
}
return null;
}
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;
}
上一篇: android 设置全屏的两种方法