从XML资源文件获取所有字符串资源?
问题描述:
我有一个名为suggestions.xml
的资源文件,该文件已翻译成多种语言.这些XML文件仅包含<string>
值.
I have a resource file called suggestions.xml
which is translated to a couple of languages. These XML files contain just <string>
values.
现在,我想检索当前语言环境的suggestions.xml
文件中的所有字符串.我怎么做?我知道我可以按ID检索单个字符串,但是我想在XML文件中获取所有 字符串.
Now, I'd like to retrieve all the strings in the current locale's suggestions.xml
file. How do I do that? I know I can retrieve single strings by their ID's, but I'd like to get all the strings in the XML file instead.
答
我不知道您为什么需要这样做.不过,您仍然可以使用生成的R
类来遍历各种资源.
I wonder why would you need to do this. Still, you can use the generated R
class to iterate over all kinds of resources.
Field[] fields = R.string.class.getFields();
String[] stringNames = new String[fields.length];
for (int i = 0; i < fields.length; i++) {
stringNames[i] = fields[i].getName();
}