Drawable resource using a variable

后端 未结 2 1649
借酒劲吻你
借酒劲吻你 2021-02-19 00:43

I want to use a variable instead of R.drawable.myimage because I have a database with the image\'s names. I get the name from the database and I want to use the drawable resourc

相关标签:
2条回答
  • 2021-02-19 01:09

    You can use this code to get the identifier...

     Resources res = this.getResources(); 
     int resID = res.getIdentifier(imagename, "drawable", this.getPackageName());
    
    0 讨论(0)
  • 2021-02-19 01:17

    You can use the name of a resource like

    getIdentifier (String name, String defType, String defPackage);

    getResources().getIdentifier("us","drawable","com.app");
    

    The above function will return an integer value same as R.drawable.us.

    This is how you access with resource names.

    0 讨论(0)
提交回复
热议问题