内容驱动的ImageView

时间:2018-10-25 15:01:39

标签: android android-studio android-imageview

我有一个 ShowActivity 和两个 Textview 和一个 imageView 。Texviews从string.xml获取资源并在ShowActivity上显示,我使IMAGEVIEW像这样。

如何通过imageview从drawble中获取每个subject_numbers的资源?

这是我的textview代码:

TextView tv2 = (TextView) findViewById(R.id.textView2);
String stringName_2 = "subject_text_" + String.valueOf(Subject_number);
int resID_2 = getResources().getIdentifier(stringName_2, "string", getPackageName());
tv2.setText(resID_2);

我想为我的Imageview编写这样的代码。我做到了,但是没用..

 ImageView img = (ImageView) findViewById(R.id.imgview_x);

 String imageName = "image_"+ String.valueOf(Subject_number);

int resID = getResources().getIdentifier(imageName,"drawable",getPackageName());
img.setImageResource(resID);

任何人都可以帮助我

1 个答案:

答案 0 :(得分:0)

您应该这样做:

int id = getResources().getIdentifier("yourpackagename:drawable/" + imageName, null, null);
imageview.setImageResource(id);