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

nif*_*ody 1 xml arrays android android-layout

我在我的string res/value文件夹中的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>
Run Code Online (Sandbox Code Playgroud)

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

我的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]"/>
Run Code Online (Sandbox Code Playgroud)

我知道它似乎会导致错误.如何在Android中的string.xml中的字符串数组上检索这些内容

ein*_*eee 8

只需给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>
Run Code Online (Sandbox Code Playgroud)

然后:

<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"/>
Run Code Online (Sandbox Code Playgroud)

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