Android用反射方式获取本地资源
作者:
小玉1991 | 来源:发表于
2020-11-24 15:31 被阅读0次(1)Android 的API:getIdentifier
int resID = resources.getIdentifier(listIcon, "drawable", context.getPackageName());
int resourceId = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
(2) 另一种方式--反射
public static Integer getIconResId(String key) {
try {
String name = key ;
Field field = R.drawable.class.getField(name);
return field.getInt(null);
} catch (SecurityException e) {
} catch (NoSuchFieldException e) {
} catch (IllegalAccessException e) {
}
return -1;
}
本文标题:Android用反射方式获取本地资源
本文链接:https://www.haomeiwen.com/subject/cqgiiktx.html
网友评论