我如何调用XML字符串布局数组值(How Do I call String-array value

2019-10-23 04:41发布

我在这样的Android RES /值夹我的string.xml文件中的字符串数组

我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>

我需要检索阵列的这样的第一要素

我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]"/>

我知道好像它会导致错误。 如何retrive字符串数组这些东西在Android的string.xml

Answer 1:

只是为了GIVA一个完整的回答你的问题:

有没有直接访问数组的方式,但你可以做这样的事情:

<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>

接着:

<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"/>

所以你基本上直接引用的neccessary串,被你的字符串数组中引用。 ;)



Answer 2:

请参考以下链接的Android -检索资源字符串数组

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

基本上,你必须要通过一个适配器在Java类阵列检索XML代码。

我张贴使用的第一个链接

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

这是如何获得从XML整个阵列的样品。 你可以修改它的单个阵列值检索,或者您希望检索哪个数组值。

快乐编码:d



文章来源: How Do I call String-array value in xml layout