【问题标题】:Setting an ImageView from the drawable folder dynamicly从可绘制文件夹中动态设置 ImageView
【发布时间】:2018-08-30 17:06:04
【问题描述】:

各位开发者,

我编写了一个名为 Item 的类。现在我希望这个“项目”从可绘制文件夹中获取构造函数中的图像。我已经尝试以某种方式从可绘制文件夹中的图像中获取 resourceId,但不知道如何,因为现在有 imageView.getResourceId() 这样的功能,无论如何这不是最好的解决方案,因为我需要临时添加所有图像从drawable到imageView,然后获取Id。有什么办法可以解决这个问题吗?

感谢您的帮助

【问题讨论】:

标签: android imageview drawable


【解决方案1】:

解决方案:

只需在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));

就是这样。希望对您有所帮助。

【讨论】:

  • 很高兴我能帮助哥们.. :)
【解决方案2】:

试试这个,

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

这将返回您要访问的drawable的ID...然后您可以通过执行以下操作在imageview中设置图像

imageview.setImageResource(id);

【讨论】:

    猜你喜欢
    • 2015-12-07
    • 2012-02-27
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 2011-02-01
    • 2016-02-06
    相关资源
    最近更新 更多