如何访问资源,在我的情况下,动态域名?我的、情况下、动态域名、资源

2023-09-05 00:11:38 作者:两袖风生

如果我得到的图像的名字像下面的变量:

  VAR imageName = SERVICE.getImg();
 

然后,我怎么能得到与 R.drawable资源。???? ,我试过 R.drawable [imageName] ,但是失败了。有什么建议?

解决方案

 内部ID = getResources()则getIdentifier(imageName,类型,封装)。
 
花生壳动态域名解析软件 新花生壳动态域名解析工具下载 V3.9.2.15544官方版 偶要下载站

This将让你你正在寻找的资源的ID 。有了它,你就可以从R类访问的资源。

If I get the image name as a variable like following:

var imageName = SERVICE.getImg();

Then, how can I get the resource with R.drawable.????, I tried R.drawable[imageName], but it failed. Any suggestions?

解决方案

int id = getResources().getIdentifier(imageName, type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.