您的位置:首页 > 移动开发 > Android开发

Android string.xml字符串的格式化和样式

2017-04-19 10:10 921 查看

字符串资源

字符串资源为您的应用提供具有可选文本样式和格式设置的文本字符串。 共有三种类型的资源可为您的应用提供字符串:

String

提供单个字符串的 XML 资源。

String Array

提供字符串数组的 XML 资源。

Quantity Strings (Plurals)

带有用于多元化的不同字符串的 XML 资源。

以下应用代码用于检索字符串:

String string = getString(R.string.hello);


使用 getString(int) 或 getText(int) 来检索字符串。getText(int) 将保留应用于字符串的任何富文本样式设置。

Quantity Strings (Plurals)

资源引用:

在 Java 中:R.plurals.plural_name


语法:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<plurals
name="plural_name">
<item
quantity=["zero" | "one" | "two" | "few" | "many" | "other"]
>text_string</item>
</plurals>
</resources>

----------

<plurals>
一个字符串集合,根据事物数量提供其中的一个字符串。 包含一个或多个 <item> 元素。


Java代码:

int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);


注意:

1.getQuantityString() 获取的字符串是会根据不同的系统语言得到的字符串也不一样。

2.使用数量字符串来替代 if 语句似乎更为方便,但必须注意的是,某些语言(如中文)根本不做这些语法区分,因此您获取的始终是 other 字符串。

3.简单说明Quantity Strings的应用场景浅见

总结:Quantity Strings在英文语义下,使用于单复数区分,比如:

<plurals name="buy_kindle">
<item quantity="one">I want to buy a Kindle</item>
<item quantity="other">I want to buy some Kindles</item>
</plurals>


格式和样式设置

在字符串包含特殊字符,以及需要格式化时,需要对字符串作特殊处理,否则得不到我们想要的效果。

转义撇号和引号

1.如果字符串中包含撇号 (‘),您必须用反斜杠 (\’) 将其转义,或为字符串加上双引号 (“”)。 例如,以下是一些有效和无效的字符串:

<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>
<!-- Causes a compile error -->


2.如果字符串中包含双引号,您必须将其转义(使用 \”)。 为字符串加上单引号不起作用。

<string name="good_example">This is a \"good string\".</string>
<string name="bad_example">This is a "bad string".</string>
<!-- Quotes are stripped; displays as: This is a bad string. -->
<string name="bad_example_2">'This is another "bad string".'</string>
<!-- Causes a compile error -->


设置字符串格式

使用 String.format(String, Object…) 设置字符串格式,可以通过在字符串资源中加入格式参数来实现。

<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>


特殊字符转义

要在string.xml 中显示特殊符号,如@,冒号,<>,字符末尾加空格等,直接写是无法正常显示的。只能考虑使用ASCII码进行显示常见字符与ASCII十进制对应表,比如:

@号 @
:号 :
空格  


XML转义字符

以下为XML标志符的数字和字符串转义符
"     (" 或 ")
'     (' 或 ')
&     (& 或 &)
lt(<) (< 或 <)
gt(>) (> 或 >)


使用 HTML 标记设置样式

支持的 HTML 元素包括:

<b>表示粗文本

<i>表示斜体文本

<u>表示下划线文本

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="welcome">Welcome to <b>Android</b>!</string>
<!-- 将带样式的文本资源存储为 HTML 转义字符串-->
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
</resources>


设置字符串格式,但还要调用 fromHtml(String) 以将 HTML 文本转换成带样式文本:

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);


由于 fromHtml(String) 方法将设置所有 HTML 实体的格式,因此务必要使用 htmlEncode(String) 对您用于带格式文本的字符串中任何可能的 HTML 字符进行转义。 例如,如果您向 String.format() 传递的字符串参数可能包含“<”或“&”之类的字符,则必须在设置格式前进行转义,这样在通过 fromHtml(String) 传递带格式字符串时,字符就能以原始形式显示出来。 例如:

String escapedUsername = TextUtil.htmlEncode(username);

Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), escapedUsername, mailCount);
CharSequence styledText = Html.fromHtml(text);


Spannable 设置样式

Spannable 是一种文本对象,让您可以使用颜色和字体粗细等字体属性进行样式设置。 您可以使用 SpannableStringBuilder 生成文本,然后对文本应用 android.text.style 包中定义的样式。

参考来源:官方文档–字符串资源

管理本地化字符串

妥善管理应用的 UI 字符串非常重要,这样您才能为用户提供优质的体验,并让本地化工作事半功倍。

1. 将所有字符串放入 strings.xml 中

如果生成带有文本的图片,也请将这些字符串放入 strings.xml,并在翻译之后重新生成图片。

2. 遵循 Android 的 UI 字符串准则

3. 为声明的字符串提供充足的上下文

在 strings.xml 文件中声明字符串时,确保清楚说明使用该字符串的上下文。 此信息对翻译人员来说十分重要,有助于提高翻译质量,也有助于您始终有效地管理字符串。

<!-- The action for submitting a form. This text is on a button that can fit 30 chars -->
<string name="login_submit_button">Sign in</string>


您需要提供的上下文信息包括:

字符串有何用途?它在何时/哪里呈现给用户?

它处于布局中的什么位置?例如,如果它是按钮,那么翻译的灵活性就不如文本框。

4. 标记不应翻译的消息部分

有时候字符串中包含不应被翻译为其他语言的文本。 常见的示例包括代码某个值的占位符、特殊符号或名称。 在准备翻译字符串时,请查找并标记应该保留原样而不需要翻译的文本,这样翻译人员就不会更改这些内容。

要标记不应翻译的文本,请使用 <xliff:g> 占位符标记。以下示例标记可确保文本“%1$s”在翻译过程中不会被更改(否则这条消息会被破坏):

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">

<!-- Example placeholder for a special unicode symbol -->
<string name="countdown">
<xliff:g id="time" example="5 days>%1$s</xliff:g>until holiday</string>

<string name="star_rating">Check out our 5

<xliff:g id="star">\u2605</xliff:g>

</string>

<!-- Example placeholder for a for a URL -->

<string name="app_homeurl">

Visit us at <xliff:g id="application_homepage">http://my/app/home.html</xliff:g>

</string>

<!-- Example placeholder for a name -->

<string name="prod_name">

Learn more at <xliff:g id="prod_gamegroup">Game Group</xliff:g>

</string>

<!-- Example placeholder for a literal -->

<string name="promo_message">

Please use the "<xliff:g id="promotion_code">ABCDEFG</xliff:g>” to get a discount.

</string>

...

</resources>


<xliff:g>标签介绍:

1.属性id可以随便命名
2.属性值举例说明
%n$ms:代表输出的是字符串,n代表是第几个参数,设置m的值可以在输出之前放置空格
%n$md:代表输出的是整数,n代表是第几个参数,设置m的值可以在输出之前放置空格,也可以设为0m,在输出之前放置m个0
%n$mf:代表输出的是浮点数,n代表是第几个参数,设置m的值可以控制小数位数,如m=2.2时,输出格式为00.00

也可简单写成:
%d   (表示整数)
%f    (表示浮点数)
%s   (表示字符串)


参考资料:

官方文档–本地化检查单

官方文档–写作风格

下一篇:app增加国际化的资源文件支持
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: