通过反射来获取资源id
try{
String resName = "ic_launcher";
int resId = R.drawable.class.getDeclaredField(resName).getInt(R.drawable.class);
imageView.setImageResource(resId);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
时间: 2024-10-20 06:17:52