如何访问资源,在我的情况下,动态域名? [英] How to access resource with dynamic name in my case?
本文介绍了如何访问资源,在我的情况下,动态域名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我得到的图像的名字像下面的变量:
VAR imageName = SERVICE.getImg();
然后,我怎么能得到与 R.drawable资源。????
,我试过 R.drawable [imageName]
,但是失败了。有什么建议?
解决方案
内部ID = getResources()则getIdentifier(imageName,类型,封装)。
<一个href="http://developer.android.com/reference/android/content/res/Resources.html#getIdentifier%28java.lang.String,%20java.lang.String,%20java.lang.String%29">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.
这篇关于如何访问资源,在我的情况下,动态域名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文