问题描述
我有preferences在这里您可以启用/禁用哪些项目将出现在菜单上。有17个项目。我做的值的字符串数组/加标题为每个17项arrays.xml。
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.
我有我的preferences文件布局preferences.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.
我怎样才能做到这一点?
How can I do this?
在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/resources/string-resource.html
您看到比下排列部(字符串数组,至少),资源引用(当你从一个XML获得)未指定的方式来解决个别项目。你甚至可以在你的XML尝试使用@阵列/ 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:
Referencing在XML数组的XML字符串(Android版)
您可以通过处理独立字符串里的数组的定义欺骗(不是真的)数组定义。例如,在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>
通过使用此,你可以使用@字符串/地球和@字符串/月亮常在你的机器人:文本和机器人:标题为XML领域,但你不会失去能力使用您打算在首位任何目的数组定义。
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字符串数组引用的资源串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!