如何在xml布局中调用String-array值?

时间:2023-01-31 07:29:18

I have a string-array in my string.xml file in Android res/value folder like this

字符串中有一个字符串数组。Android res/value文件夹中的xml文件如下

my string.xml

<string-array name="books_titles_en">
    <item>Smoking Effeccts on Your Body</item>
    <item>Smoking - effects on your body</item>
    <item>How Tobacco Smoke Causes Disease</item>
    <item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>

I need to retrieve the first element of array like this

我需要像这样检索数组的第一个元素

my home.xml

 <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/frag_1_text_pad"
                android:textSize="@dimen/frag_1_text_size"
                android:paddingRight="@dimen/frg_1_text_right_pad"
                android:id="@+id/book_link0"
                android:text="@string-array/book_title_en[0]"/>

I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android

我知道这似乎会导致错误。如何在字符串中的字符串数组中检索这些东西。xml在安卓

2 个解决方案

#1


6  

Just to giva a full answer to your problem:

给你的问题一个完整的答案:

There is no way of accessing an array directly, but you could do something like this:

没有直接访问数组的方法,但是您可以这样做:

<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
    <item>@string/books_titles_en_1</item>
    <item>@string/books_titles_en_2</item>
    <item>@string/books_titles_en_3</item>
    <item>@string/books_titles_en_4</item>
</string-array>

and then:

然后:

<TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/frag_1_text_pad"
            android:textSize="@dimen/frag_1_text_size"
            android:paddingRight="@dimen/frg_1_text_right_pad"
            android:id="@+id/book_link0"
            android:text="@string/book_title_en_1"/>

So you basically reference the neccessary strings directly, that are referenced in your string array. ;)

所以你基本上直接引用了必要的字符串,这是在你的字符串数组中引用的。,)

#2


0  

Refer to this link Android - retrieve string array from resources

参考这个链接——从参考资料中检索字符串数组

And this link Help in getting String Array from arrays.xml file

这个链接帮助从数组中获取字符串数组。xml文件

Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.

基本上,您必须通过创建适配器来为Java类中的数组检索xml编写代码。

The first link I posted used

我发布的第一个链接是用的

for(int i = 0; i < menuArray.length; i++) 
    {
        Button b = new Button(this);
        b.setText(menuArray[i]);
        ll.addView(b);
    }

which is a sample of how to obtain the whole array from the xml. You can modify it for single array value retrieval, or whichever array value you wish to retrieve.

这是如何从xml获取整个数组的示例。您可以为单个数组值检索或您希望检索的任何数组值修改它。

Happy coding :D

快乐编码:D

#1


6  

Just to giva a full answer to your problem:

给你的问题一个完整的答案:

There is no way of accessing an array directly, but you could do something like this:

没有直接访问数组的方法,但是您可以这样做:

<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
    <item>@string/books_titles_en_1</item>
    <item>@string/books_titles_en_2</item>
    <item>@string/books_titles_en_3</item>
    <item>@string/books_titles_en_4</item>
</string-array>

and then:

然后:

<TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="@dimen/frag_1_text_pad"
            android:textSize="@dimen/frag_1_text_size"
            android:paddingRight="@dimen/frg_1_text_right_pad"
            android:id="@+id/book_link0"
            android:text="@string/book_title_en_1"/>

So you basically reference the neccessary strings directly, that are referenced in your string array. ;)

所以你基本上直接引用了必要的字符串,这是在你的字符串数组中引用的。,)

#2


0  

Refer to this link Android - retrieve string array from resources

参考这个链接——从参考资料中检索字符串数组

And this link Help in getting String Array from arrays.xml file

这个链接帮助从数组中获取字符串数组。xml文件

Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.

基本上,您必须通过创建适配器来为Java类中的数组检索xml编写代码。

The first link I posted used

我发布的第一个链接是用的

for(int i = 0; i < menuArray.length; i++) 
    {
        Button b = new Button(this);
        b.setText(menuArray[i]);
        ll.addView(b);
    }

which is a sample of how to obtain the whole array from the xml. You can modify it for single array value retrieval, or whichever array value you wish to retrieve.

这是如何从xml获取整个数组的示例。您可以为单个数组值检索或您希望检索的任何数组值修改它。

Happy coding :D

快乐编码:D