我想根据我的字符串更改imageview src,我有这样的东西:
ImageView imageView1 = (ImageView)findViewById(R.id.imageView1); String correctAnswer = "poland"; String whatEver = R.drawable+correctAnswer; imageView1.setImageResource(whatEver);
当然不行.如何以编程方式更改图像?
解决方法
public static int getImageId(Context context,String imageName) { return context.getResources().getIdentifier("drawable/" + imageName,null,context.getPackageName()); }
使用:
imageView1.setImageResource(getImageId(this,correctAnswer);
注意:离开扩展名(例如“.jpg”).
示例:image是“abcd_36.jpg”
Context c = getApplicationContext(); int id = c.getResources().getIdentifier("drawable/"+"abcd_36",c.getPackageName()); ((ImageView)v.findViewById(R.id.your_image_on_your_layout)).setImageResource(id);