按名称获取字符串资源字符串、名称、资源

由网友(心不饶人嘴上甜,)分享简介:我已经看到了这个问题,几个线程,但我不知道为什么他们没有为我工作。在这里,短期code:I have seen several threads about this question but I don't know why they are not working for me. Here the short co...

我已经看到了这个问题,几个线程,但我不知道为什么他们没有为我工作。在这里,短期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);
阅读全文

相关推荐

最新文章