你好那里的开发人员,
我写了一个名为 Item 的类。现在我希望这个“项目”从可绘制文件夹中获取构造函数中的图像。我已经尝试以某种方式从可绘制文件夹中的图像中获取 resourceId,但不知道如何,因为现在有这样的功能imageView.getResourceId()无论如何都不是最好的解决方案,因为我需要暂时将所有图像从drawable添加到一个 imageView,然后获取 Id。有没有办法解决这个问题?
谢谢您的帮助
解决方案:
只需在 java 中使用此代码将可绘制图像动态设置为 ImageView:
your_image_view_object.setImageDrawable(ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_drawable_image));
例如:
imageView.setImageDrawable(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_profilepic));
而已。希望能帮助到你。
尝试这个,
int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);
这将返回您要访问的可绘制对象的 id...然后您可以通过执行以下操作在 imageview 中设置图像
imageview.setImageResource(id);