2011-07-04 39 views
15

值/ string.xml剪断代码:是否可以在Android中访问字符串数组项?

<array name="categories"> 
    <item name="today">Today</item> 
    <item name="life">Life</item> 
    <item name="corner">Corner</item> 
    <item name="banks">Banks</item> 
    <item name="it">IT</item> 
    <item name="fun">Fun</item> 
</array> 

剪断代码从布局/ main.xml中

<Button 
    android:id="@+id/today" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textSize="8sp" 
    android:text="@array/categories" 
    /> 

是否有可能一个标签名称分配给一个按钮id =今天直接从值/ string.xml使用字符串数组与名称=类别和特定的ite m,在这种情况下 - 今天

+0

啊,所以你的意思是机器人:文本=“@阵列/类别[0 ]“(这是不正确的),但这就是你想要的吗?试过android:text =“@ array/categories.today”? – Blundell

+0

这是正确的,我的意思!但'android:text =“@ array/categories.today”'不起作用,其他猜测? – nenito

回答

19

查看所选答案在这里:Android - reference a string in a string array resource with 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> 

,那么只需在做:

<Button .... 
    android:text="@string/earth" /> 
+3

我想你误解了我。我想获取在** values/string.xml **文件中声明的单个数组项的值,并将该值用于按钮的文本。 'android:text =“@ array/categories/”' – nenito

+4

对,上面显示的技术显示了如何做到这一点。你只需要改变你的数组值就像上面那样构造。再看看并尝试一下。阅读我发布的链接中的答案。 – Fraggle

+2

所以,实际上这样做是不可能的,因为它们都没有任何意义:' Earth Moon'和 '<字符串数组名=“系统”> @串/大地 @串/月亮 ' – nenito

相关问题