final ImageView img =findViewById(R.id.imageView1);
int id = getResId("图片文件名", R.drawable.class);
img.setImageResource(id);
int getResId(String variableName, Class> c)
{
try {
Field idField = c.getDeclaredField(variableName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}


![[android]根据文件名动态设置ImageView图片 [android]根据文件名动态设置ImageView图片](http://www.mshxw.com/aiimages/31/351275.png)
