使用 xml 引用字符串数组资源中的字符串 [英] Referencing a string in a string array resource with xml

查看:56
本文介绍了使用 xml 引用字符串数组资源中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有偏好设置,您可以在其中启用/禁用将在菜单上显示的项目.有17个项目.我在 values/arrays.xml 中创建了一个字符串数组,其中包含这 17 个项目中每一个的标题.

I have preferences where you can enable/disable what items will show up on the menu. There are 17 items. I made a string array in values/arrays.xml with titles for each of these 17 items.

我有首选项.xml,其中包含我的首选项文件的布局,我想从字符串数组中引用一个项目作为标题.

I have preferences.xml which has the layout for my preferences file, and I would like to reference a single item from the string array to use as the title.

我该怎么做?

在 Android 开发人员参考中,我看到了如何使用 XML 引用单个字符串,但没有看到如何从 XML 中的数组资源引用字符串.

In the Android developer reference, I see how I can reference a single string with XML, but not how I can reference a string from an array resource in XML.

推荐答案

简而言之:我不认为你可以,但似乎有一个解决方法:.

如果您查看此处的 Android 资源:

If you take a look into the Android Resource here:

http://developer.android.com/guide/topics/资源/字符串资源.html

您会看到,在数组部分(至少是字符串数组)下,资源引用"(正如您从 XML 中获得的那样)没有指定寻址各个项目的方法.您甚至可以尝试在您的 XML 中使用@array/yourarrayhere".我知道在设计时你会得到第一个项目.但如果你想使用,那是没有实际用处的,比方说......当然是第二个.

You see than under the array section (string array, at least), the "RESOURCE REFERENCE" (as you get from an XML) does not specify a way to address the individual items. You can even try in your XML to use "@array/yourarrayhere". I know that in design time you will get the first item. But that is of no practical use if you want to use, let's say... the second, of course.

但是,您可以使用一个技巧.请看这里:

HOWEVER, there is a trick you can do. See here:

在 XML 中引用 XML 字符串数组(安卓)

您可以通过寻址数组定义中的独立字符串来欺骗"(不是真的)数组定义.例如,在您的 strings.xml 中:

You can "cheat" (not really) the array definition by addressing independent strings INSIDE the definition of the array. For example, in your strings.xml:

<string name="earth">Earth</string>
<string name="moon">Moon</string>

<string-array name="system">
    <item>@string/earth</item>
    <item>@string/moon</item>
</string-array>

通过使用这个,你可以在你的android:text"和android:title"XML字段中正常使用@string/earth"和@string/moon",但你不会失去将数组定义用于您最初打算的任何目的.

By using this, you can use "@string/earth" and "@string/moon" normally in your "android:text" and "android:title" XML fields, and yet you won't lose the ability to use the array definition for whatever purposes you intended in the first place.

似乎在我的 Eclipse 上工作.你为什么不试试告诉我们它是否有效?:-)

Seems to work here on my Eclipse. Why don't you try and tell us if it works? :-)

这篇关于使用 xml 引用字符串数组资源中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆