Android的 - 从@drawable字符串打开资源字符串、资源、Android、drawable

由网友(迷茫的生活)分享简介:我有:String uri = "@drawable/myresource.png";我如何可以加载在ImageView的? this.setImageDrawable?How can I load that in ImageView? this.setImageDrawable?推荐答案如果你真的需要一个字符串...

我有:


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);
阅读全文

相关推荐

最新文章