Android的 - 从@drawable字符串打开资源 [英] Android - Open resource from @drawable String

查看:196
本文介绍了Android的 - 从@drawable字符串打开资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:


String uri = "@drawable/myresource.png";

我如何可以加载在ImageView的? this.setImageDrawable?

How can I load that in ImageView? this.setImageDrawable?

推荐答案

如果你真的需要一个字符串来工作,尝试是这样的:

If you really need to work with a string, try something like this:

private void showImage() {
    String uri = "drawable/icon";

    // int imageResource = R.drawable.icon;
    int imageResource = getResources().getIdentifier(uri, null, getPackageName());

    ImageView imageView = (ImageView) findViewById(R.id.myImageView);
    Drawable image = getResources().getDrawable(imageResource);
    imageView.setImageDrawable(image);
}

否则我会建议您一起与R *是这样引用:

Else I would recommend you to work with R.* references like this:

  int imageResource = R.drawable.icon;
  Drawable image = getResources().getDrawable(imageResource);

这篇关于Android的 - 从@drawable字符串打开资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆