1,
Context context = getBaseContext();
int id = context.getResources().getIdentifier(ImageName, "mipmap", context.getPackageName());
2,
try {
Field field = R.mipmap.class.getDeclaredField(ImageName);
field.setAccessible(true);
int id = field.getInt(field.getName());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
3,
try {
Field field = R.mipmap.class.getDeclaredField(ImageName);
field.setAccessible(true);
R.mipmap mipmap = new R.mipmap();
Object oId = field.get(mipmap);
int id = (Integer) oId;
} catch (Exception e) {
e.printStackTrace();
}
本文介绍了三种在Android应用中通过字符串名称获取图片资源ID的方法。这些方法包括使用getBaseContext()获取上下文,反射机制直接从R.mipmap中获取资源ID,以及创建R.mipmap实例并通过反射获取资源ID。
1500

被折叠的 条评论
为什么被折叠?



