句法:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="string_name"
>text_string</string>
</resources>
ELEMENTS:<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello!</string> </resources>
这种布局XML应用一个字符串视图:<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
此应用程序代码检索字符串:String string = getString
(R.string.hello);
您可以使用的getString(INT)和gettext(INT)来检索字符串。的getText(INT)将保留应用到任何字符串丰富的文本样式。<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="string_array_name"> <item >text_string</item> </string-array> </resources>
ELEMENTS:<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array> </resources>
此应用程序代码检索一个字符串数组:Resources res = getResources()
; String[] planets = res.getStringArray
(R.array.planets_array);
数量字符串(复数)<?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>
ELEMENTS:<?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfSongsAvailable"> <!-- As a developer, you should always supply "one" and "other" strings. Your translators will know which strings are actually needed for their language. Always include %d in "one" because translators will need to use %d for languages where "one" doesn't mean 1 (as explained above). --> <item quantity="one">%d song found.</item> <item quantity="other">%d songs found.</item> </plurals> </resources>
XML file saved at res/values-pl/strings.xml
:
?xml version="1.0" encoding="utf-8"?> <resources> <plurals name="numberOfSongsAvailable"> <item quantity="one">Znaleziono %d piosenkę.</item> <item quantity="few">Znaleziono %d piosenki.</item> <item quantity="other">Znaleziono %d piosenek.</item> </plurals> </resources>Java code:
int count = getNumberOfsongsAvailable();
Resources res = getResources()
;
String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count, count);
当使用getQuantityString()方法中,你需要通过计数两次,如果你的字符串包含字符串以数字格式。例如,对于发现字符串%d首歌曲,所述第一计数参数选择适当的复数串和所述第二计数参数被插入%d个占位符。如果您的多个琴弦不包括字符串格式化,你并不需要第三个参数传递给getQuantityString。
<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 -->如果你在你的强大的双引号,你必须转义(\“)。周围使用单引号括起来的不起作用。
<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 name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>在这个例子中,格式字符串有两个参数:%1 $ s是一个字符串%2 $ d是一个十进制数。您可以格式化从您的应用程序像这样的参数字符串:
Resources res = getResources()
;
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
与HTML标记的样式
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="welcome">Welcome to <b>Android</b>!</string> </resources>
Supported HTML elements include:
<b>
for bold text.<i>
for italic text.<u>
for underline text.<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string> </resources>在这个格式化字符串,A <B>元素添加。请注意,打开支架HTML转义,使用的&lt; 符号。
Resources res = getResources()
;
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
CharSequence styledText = Html.fromHtml(text);
因为fromHtml(String)方法将格式化所有HTML实体,一定要逃避你格式化文本使用的字符串的任何可能的HTML字符,使用的HTMLEncode(字符串)。举例来说,如果你将传递一个字符串参数的String.format()可能包含字符,如“<”或“&”,那么他们必须在格式化之前,这样,当格式化字符串通过fromHtml传递逃脱, (字符串),人物出来最初编写的方式。 例如: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);
/** * Returns a CharSequence that concatenates the specified array of CharSequence * objects and then applies a list of zero or more tags to the entire range. * * @param content an array of character sequences to apply a style to * @param tags the styled span objects to apply to the content * such as android.text.style.StyleSpan * */ private static CharSequence apply(CharSequence[] content, Object... tags) { SpannableStringBuilder text = new SpannableStringBuilder(); openTags(text, tags); for (CharSequence item : content) { text.append(item); } closeTags(text, tags); return text; } /** * Iterates over an array of tags and applies them to the beginning of the specified * Spannable object so that future text appended to the text will have the styling * applied to it. Do not call this method directly. */ private static void openTags(Spannable text, Object[] tags) { for (Object tag : tags) { text.setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK); } } /** * "Closes" the specified tags on a Spannable by updating the spans to be * endpoint-exclusive so that future text appended to the end will not take * on the same styling. Do not call this method directly. */ private static void closeTags(Spannable text, Object[] tags) { int len = text.length(); for (Object tag : tags) { if (len > 0) { text.setSpan(tag, 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); } else { text.removeSpan(tag); } } }下面的粗体,斜体和颜色的方法告诉你如何调用helper方法适用于android.text.style包中定义的样式。您可以创建类似的方法做其他类型的文本造型
/** * Returns a CharSequence that applies boldface to the concatenation * of the specified CharSequence objects. */ public static CharSequence bold(CharSequence... content) { return apply(content, new StyleSpan(Typeface.BOLD)); } /** * Returns a CharSequence that applies italics to the concatenation * of the specified CharSequence objects. */ public static CharSequence italic(CharSequence... content) { return apply(content, new StyleSpan(Typeface.ITALIC)); } /** * Returns a CharSequence that applies a foreground color to the * concatenation of the specified CharSequence objects. */ public static CharSequence color(int color, CharSequence... content) { return apply(content, new ForegroundColorSpan(color)); }这里有一个如何来链接这些方法来创建应用到各个单词的不同类型的造型的字符序列的例子
// Create an italic "hello, " a red "world", // and bold the entire sequence. CharSequence text = bold(italic(res.getString(R.string.hello)), color(Color.RED, res.getString(R.string.world)));