使用 strings.xml 文件
字符串资源为应用程序提供可选的文本样式和格式的文本字符串。有三种类型的资源可以为你的应用程序提供字符串:
字符串
XML resource that provides a single string.
句法:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string_name">text_string</string>
</resources>
并在布局中使用此字符串:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/string_name" />
字符串数组
XML resource that provides an array of strings.
句法:
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
用法
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
数量字符串(复数)
XML resource that carries different strings for pluralization.
句法:
<?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>
用法:
int count = getNumberOfsongsAvailable();
Resources res = getResources();
String songsFound = res.getQuantityString(R.plurals.plural_name, count, count);