How to replace R.drawable.“someString”

后端 未结 6 981
长发绾君心
长发绾君心 2021-02-02 10:00

I have some Images in my project. The name of the image is stored in a String and I would like to setImageResource(R.drawable.\".....\"); with the string of the image name but t

6条回答
  •  再見小時候
    2021-02-02 10:38

    try this

       String uri = "NAME";
    
        // 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);
    

提交回复
热议问题