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

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

问题描述

我一直无法找到一个解决方案。

I have been unable to find a solution for this.

我想要做什么:我有preferences,您可以启用/禁用哪些项目将出现在菜单上。共有17个项目。我做了一个字符串数组中的值/ arrays.xml以标题为每种17个项目。

What I'm trying to do: 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.xml这对我的preferences文件中的布局,我想引用单个项目从字符串数组作为标题来使用。

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, if it's possible, can I do this?

在Android开发的参考,我看我怎么可以参考一个字符串,XML,但现在我怎么可以从XML阵列资源引用的字符串。

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

感谢

推荐答案

在短期:我不认为你可以,但似乎有一个解决方法:

如果你看看到了Android资源在这里:

If you take a look into the Android Resource here:

<一个href="http://developer.android.com/guide/topics/resources/string-resource.html">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:

<一个href="http://stackoverflow.com/questions/2863827/referencing-an-xml-string-in-an-xml-array-android/2865276#2865276">Referencing在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>

通过使用此,你可以使用@字符串/地球和@字符串/月通常你的机器人:文本和机器人:标题为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? :-)

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

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