按名称获取字符串资源 [英] Getting string resource by name
本文介绍了按名称获取字符串资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经看到了这个问题,几个线程,但我不知道为什么他们没有为我工作。在这里,短期code:
I have seen several threads about this question but I don't know why they are not working for me. Here the short code:
//This doesn't work. I'm getting a 0 id
id=getResources().getIdentifier("q1", "id", getPackageName());
//This retrieves the string correctly (just to prove the string is called q1
String str=getResources().getString(R.string.q1);
我能想到的唯一的事情就是与包名有问题,但似乎好了。
The only thing I could think about is a problem with the packagename, but it seems Ok.
任何帮助吗?
推荐答案
获取资源方式名称
String packageName = "YOUR_PACKAGE_NAME";
String abString="Resources_Name";
int resId = getResources().getIdentifier(abString, "string", packageName);
//OR
String aString="Resources_Name";
int resId = getResources().getIdentifier(aString, null, null);
这篇关于按名称获取字符串资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文