【发布时间】:2020-09-25 17:52:12
【问题描述】:
我该如何替换它
Drawable drawable = getResources().getDrawable(R.drawable.imagesample);
字符串img的值? (String img的值为res drawable中图片的名称)
我的代码:
String img = "sampleimage";
// code for image in pop-up dialog
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
View subView = inflater.inflate(R.layout.dialog, null);
final ImageView subImageView = (ImageView) subView.findViewById(R.id.image);
Drawable drawable = getResources().getDrawable(R.drawable.imagesample);
subImageView.setImageDrawable(drawable);
// code for a pop-up dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Congratulations");
builder.setMessage("Thanks for helping me!");
builder.setView(subView);
builder.setPositiveButton("Ok", null);
builder.show();
【问题讨论】:
-
当前代码有什么问题?我什至不太确定你想在这里做什么?
-
我想使用变量的值,而不是使用图像资源的直接 id @s_o_m_m_y_e_e
-
您无法以您想要的方式访问资源文件。您当前的代码是完美的,并且会给出完美的结果。
-
是的。我知道它工作得很好,我只想知道是否有部分代码可以替换,以便使用字符串值作为源而不是资源 id。
标签: java android string drawable