Usage 1: normal string
<?xml version="1.0" encoding="utf-8"?><resources> <string name="string_name">text_string</string></resources>
Usage 2:string Array
<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="string_array_name"> <item>text_string</item> </string-array></resources>
Used in Java code:
Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array);
Usage 3:plurals
<?xml version="1.0" encoding="utf-8"?><resources> <plurals name="numberOfSongsAvailable"> <item quantity="one">One song found.</item> <item quantity="other">%d songs found.</item> </plurals></resources>
Java code:
int count = getNumberOfsongsAvailable();Resources res = getResources();String songsFound = res.getQuantityString(R.plurals.numberOfSongsAvailable, count);
Usage 4:formatting and styling formatting, placeholder, etc.
1. Formatting strings
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>//java代码Resources res = getResources();String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
2.html
<?xml version="1.0" encoding="utf-8"?><resources> <string name="welcome">Welcome to <b>Android</b>!</string></resources>
3. The combination of the above two methods
<resources> <string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string></resources>Resources res = getResources();String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);CharSequence styledText = Html.fromHtml(text);
Some other uses of Strings.xml in Android