开发者

Custom Resource name Android

I could not find an answer to this question anywhere so I have posted it.

Let's say I have multiple resource strings, like:

<string name="hello">Hello World, MainScreen!</string>
<string name="app_name">My Title</string>

And I reference them in my code like so:

R.values.hello
R.values.app_name

But how can I access them like this?

String test = "hello";
String value = R.values.test;

I am trying to do something like this, but on a much larger s开发者_开发百科cale. Or is there a different, but better way of doing it?


In an Activity, this will work...

String value = getString(R.values.hello);

R.values.hello is an integer used as a 'lookup' for the actual string itself. All resources are handled this way.


The correct way to obtain those values would be

String name = CurrentClassName.this.getString(R.string.hello);

By using CurrentClassName.this you will assure that you can use it inside functions and nested classes.


I think this link might help you http://steven.bitsetters.com/2007/11/27/accessing-android-resources-by-name-at-runtime/

int id = getResources().getIdentifier("hello", "values", getPackageName());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜