前言
声明
欢迎转载,但请保留文章原始出处:)
resources types - string
译者署名: 呆呆大虾
版本:android 4.0 r1
原文
<a href="http://developer.android.com/guide/topics/resources/string-resource.html">http://developer.android.com/guide/topics/resources/string-resource.html</a>
字符串资源
string资源为应用程序提供字符串文本,并可附带文本样式和格式。有以下三种类型的string资源可供应用程序使用:
<a href="http://www.cnblogs.com/over140/admin/editposts.aspx?postid=2218560#string">string</a>
提供单个字符串的xml资源。
<a href="http://www.cnblogs.com/over140/admin/editposts.aspx?postid=2218560#string_array">string array</a>
提供字符串数组的xml资源。
<a href="http://www.cnblogs.com/over140/admin/editposts.aspx?postid=2218560#quantity_strings">quantity strings (复数)</a>
保存单词或词组各种数量形式(单数/复数)下的字符串xml资源。
<a>string</a>
能被应用程序或者其他资源文件(比如layout xml)引用的单个字符串。
注意:字符串是简单类型资源,是用名称(name)(而非xml文件名)来直接引用的。因此,在一个xml文件里,可以把字符串资源和其他简单类型资源一起放在一个<resources>元素下。
文件位置
res/values/filename.xml
文件名可随意指定。<string>元素的name将被用作资源id。
编译后资源的数据类型
资源引用
java代码::r.string.string_name
xml代码:@string/string_name
语法
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string_name">text_string</string>
</resources>
元素
必填项。必须是根元素。
无属性。
<string>
属性:
name
string类型。字符串的名称。此名称将作为资源id用于引用此字符串。
示例
保存在res/values/strings.xml 的xml文件:
<string name="hello">hello!</string>
以下布局(layout)xml将字符串资源用于view:
<textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
以下应用程序代码读取一个字符串资源:
<a>string array</a>
可被应用程序引用的字符串数组。
注意:字符串数组是简单类型资源,是用名称(而非xml文件名)来直接引用的。因此,在一个xml文件里,可以把字符串数组资源和其他简单类型资源一起放入一个<resources>元素下。
文件名可随意指定。<string-array>元素的名字name将被用作资源id。
java代码: r.array.string_array_name
<?xml version="1.0" encoding="utf-8"?>
<string-array
name="string_array_name">
<item
>text_string</item>
</string-array>
<string-array>
定义一个字符串数组。包含了一个或多个<item>元素。
string类型。字符串数组的名称。此名称将作为资源id用于引用此数组。
<item>
<string-array name="planets_array">
<item>mercury</item></string-array>
<item>venus</item>
<item>earth</item>
<item>mars</item>
以下程序代码返回字符串数组:
quantity strings(复数)
不同的语言对数量进行描述的语法规则也不同。比如在英语里,数量1是个特殊情况,我们写成“1 book”,但其他任何数量都要写成“n books”。这种单复数之间的区别是很普遍的,不过其他语言会有更好的区分方式。android支持的全集包括zero、one、 two、few、many和other。
注意,要按照语法规则来建立可选项。在英语里,即使数量为0,字符串零(zero)也不需要建立。因为0在英语语法表达上和2没有区别,和其他除1以外的任何数字都没有差别("zero books", "one book", "two books",等等)。 不要被“two听起来似乎只能用于数量2” 样的事误导。某语言可能需要2、12、102(等等)和1一样形式表示,而又与其他数的形式都不同。请依靠翻译人员来了解一下实际语言中的差别。
如果和应用程序的风格一致,常可以用诸如“books: 1”的模糊数量形式来避免使用数量字符串。这会让你和翻译人员都轻松些。
注意:复数字符串是简单类型资源,是用名称(name)(而不是xml文件名)来直接引用的。因此,在一个xml文件里,可以把复数字符串资源和其他简单类型资源一起放在一个<resources>元素下。
文件名可随意指定。<plurals>元素的名字name将被用作资源id。
java代码::r.plurals.plural_name
name="plural_name">
<item_plurals
quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
</plurals>
<plurals>
一个字符串集,每个数量表示提供一个字符串。包含一个或多个<item>元素。
string类型。字符串对的名称。此名称将作为资源id。
quantity
关键字。 表示要使用此字符串的数量值。以下是合法的值(括号内列出部分语言要求):
值
描述
zero
语言需要对数字0进行特殊处理。(比如阿拉伯语)
one
语言需要对类似1的数字进行特殊处理。(比如英语和其它大多数语言里的1;在俄语里,任何以1结尾但不以11结尾的数也属于此类型。)
two
语言需要对类似2的数字进行特殊处理。(比如威尔士语)
few
语言需要对较小数字进行特殊处理(比如捷克语里的2、3、4;或者波兰语里以2、3、4结尾但不是12、13、14的数。)
many
语言需要对较大数字进行特殊处理(比如马耳他语里以11-99结尾的数)
other
语言不需要对数字进行特殊处理。
存放在res/values/strings.xml 的xml文件:
<plurals name="numberofsongsavailable">
<item quantity="one">one song found.</item>
<item quantity="other">%d songs found.</item>
存放在res/values-pl/strings.xml 的xml文件:
<item quantity="one">znaleziono jedną piosenkę.</item>
<item quantity="few">znaleziono %d piosenki.</item>
<item quantity="other">znaleziono %d piosenek.</item>
java代码:
int count = getnumberofsongsavailable();
格式化和样式化
关于如何格式化和样式化字符串资源,下面这些比较重要的地方应该了解。
单引号和双引号的转义
如果字符串里包含单引号或双引号,必须进行转义,或者把整个串封闭在与当前引号不同的成对的引号内。下面是一些有效或无效的字符串示例:
<string name="good_example">"this'll work"</string>
<string name="good_example_2">this\'ll also work</string>
<string name="bad_example">this doesn't work</string>
<string name="bad_example_2">xml encodings don’t work</string>
字符串的格式化
<string name="welcome_messages">hello, %1s!youhaves!youhaved new messages.</string>
此例中存在两个参数:%1$s是个字符串,%2$d 是个数字。则在应用程序中可以用如下方式用参数来格式化字符串:
用html标记来样式化
可以用html 标记来为字符串加入样式。例如:
<string name="welcome">welcome to <b>android</b>!</string>
支持以下html元素:
· <b>文本加粗bold。
· <i>文本变斜体italic。
· <u>文本加下划线underline。
1. 将样式化的文本资源存储为转义后的html字符串:
<string name="welcome_messages">hello, %1s! you have &lt;b>%2s! you have &lt;b>%2d new messages&lt;/b>.</string>
在这个格式化字符串里,加入了一个<b>元素。注意左尖括号是用标记&lt转义过的html。
charsequence styledtext = html.fromhtml(text);
转载:http://www.cnblogs.com/over140/archive/2011/10/20/2218560.html