本文介绍了如何在xml布局中调用字符串数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我这样的Android res/value文件夹中的string.xml文件中有一个字符串数组
I have a string-array in my string.xml file in Android res/value folder like this
<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
<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]"/>我知道这似乎会导致错误.如何在Android的string.xml中的字符串数组上检索这些东西
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
推荐答案只为您的问题提供完整的答案:
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>然后:
<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. ;)
更多推荐
如何在xml布局中调用字符串数组值
发布评论