设置字符串格式

1
<string name="welcome_messages">Hi, %1$s! You have %2$d new messages.</string>  
1
var text = getString(R.string.welcome_messages, "kalaqiae", 10)  

输出:Hi, kalaqiae! You have 10 new messages.

%d (表示整数)
%f (表示浮点数)
%s (表示字符串)

字符串数组

1
2
3
4
5
6
<string-array name="planets_array">  
    <item>Mercury></item>  
    <item>Venus></item>  
    <item>Earth></item>  
    <item>Mars></item>  
</string-array>
1
val array: Array = resources.getStringArray(R.array.planets_array)

复数

支持以下完整集合:zero、one、two、few、many 和 other

1
2
3
4
<plurals name="numberOfSongsAvailable">
    <item quantity="one">%d song available.</item>
    <item quantity="other">%d songs available.</item>
</plurals>
1
2
val count = getNumberOfSongsAvailable()
val songsFound = resources.getQuantityString(R.plurals.numberOfSongsAvailable, count, count)

特殊字符

1
2
3
4
5
@号 &#064;
:号 &#058;
空格 &#160;
lt(<) (&#60; 或 &lt;)
gt(>) (&#62; 或 &gt;)