使用 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);