首页 > 代码库 > 3.App Resources-Resource Types/String Resources
3.App Resources-Resource Types/String Resources
1. String
//saved at res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello!</string></resources>
//This layout XML applies a string to a View:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
This application code retrieves a string:
String string = getString(R.string.hello);
2. String Array
//saved at res/values/strings.xml<?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>
This application code retrieves a string array:
Resources res = getResources();String[] planets = res.getStringArray(R.array.planets_array);
3. Formatting and Styling
3.1 Escaping apostrophes and quotes
//good<string name="good_example">"This‘ll work"</string><string name="good_example_2">This\‘ll also work</string>//bad<string name="bad_example">This doesn‘t work</string><string name="bad_example_2">XML encodings don't work</string>
3.2 Formatting strings
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments:
%1$s
is a string and %2$d
is a decimal number.
You can format the string with arguments from your application like this:
Resources res = getResources();String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
3.3 Styling with HTML markup
<?xml version="1.0" encoding="utf-8"?><resources> <string name="welcome">Welcome to <b>Android</b>!</string></resources>
<b> for Bold text
<i> for italic text
<u> for underline text
Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won‘t work
<1>Store your styled text resource as an HTML-escaped string:
//In this formatted string, a<b>
element is added. Notice that the opening bracket is HTML-escaped, using the<
notation.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
<2>Then format the string as usual, but also call fromHtml(String)
to convert the HTML text into styled text:
Resources res = getResources();String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);CharSequence styledText = Html.fromHtml(text);
<3>Particularly. if you‘ll be passing a string argument to String.format()
that may contain characters such as "<" or "&", then they must
be escaped before formatting, so that when the formatted string is passed through fromHtml(String)
, the characters come out the way
they were originally written. For example:
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);
3.App Resources-Resource Types/String Resources