如何在xml布局中调用字符串数组值

编程入门 行业动态 更新时间:2024-10-27 12:27:13
本文介绍了如何在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布局中调用字符串数组值

本文发布于:2023-10-10 11:27:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1478534.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   字符串   布局   如何在   xml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!