简体   繁体   English

在 strings.xml 中获取字符串数组的名称/标题

[英]Get name/title of a string-array in strings.xml

I've got a string-array我有一个字符串数组

<string-array name="color">
    <item>red</item>
    <item>green</item>
    <item>purple</item>
</string-array>
  1. How could I get it's name with Java?我怎么能用 Java 得到它的名字? (I want to get a string with "color" inside) (我想得到一个里面有“颜色”的字符串)

  2. How to I extract it's name to a string?如何将其名称提取到字符串? To do smth like this:这样做:

     <string-array name="@string/color"> <item>red</item> <item>green</item> <item>purple</item> </string-array>

<string name="color">color</string>

  1. Is it redudant to extract all items of a string-array to strings?将字符串数组的所有项目提取为字符串是否是多余的? like this:像这样:

     <string-array name="color"> <item>@string/red</item> <item>@string/green</item> <item>@string/purple</item>

<string name="red">red</string> <string name="green">green</string> <string name="purple">purple</string>

  1. From the documentation :文档中:

    Resources res = getResources();资源 res = getResources(); String[] colors = res.getStringArray(R.array.color);字符串[] colors = res.getStringArray(R.array.color);

  2. As far as I know, you can not do this.据我所知,你不能这样做。

  3. It depends on how you want do use the array.这取决于你想如何使用数组。 For example, if you have more than one language in your app, it makes sense to give the item a value that is a reference to another string resource.例如,如果您的应用程序中有多种语言,则为项目提供一个值是对另一个字符串资源的引用是有意义的。

  1. getResources().getResourceEntryName(R.array.variant);
  1. I can't translate strings without extracting them like so.如果不像这样提取它们,我就无法翻译字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM