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 = 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 == Res.getstringarray (R.array.planets_array);
3. Formatting and Styling
3.1 Escaping apostrophes and quotes
//Good<stringName="Good_example">"This ' ll work"</string><stringName="good_example_2">this\'ll also work</string>// Bad<stringName="Bad_example">this doesn'T work</string><stringName="bad_example_2">xml Encodings Don't work</string>
3.2 Formatting strings
<string name="welcome_messages">hello,%1$s! You have%2new messages.</string>
In this example, the format string has the arguments:
%1$s
is a string and is %2$d
a decimal number.
You can format the string with arguments from your application like this:
Resources res == 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 want to create a styled text resource, is also used as a format string. Normally, this won ' t work
<1>store your styled text resource as an html-escaped string:
<b>
<
notation.
<string name="welcome_messages">hello,%1$s! You have <b>%2new messages</b>.</string>
<2>then format the string as usual, but also call to fromHtml(String)
convert the HTML text into styled text:
Resources res == String.Format (res.getstring (R.String= html.fromhtml (text);
<3>particularly. If you'll be passing a string argument to String.format()
the May contain characters such as "<" or "& ", then they must
Be escaped before formatting, so if the formatted string is passed through fromHtml(String)
, the characters come out the
They were originally written. For example:
escapedusername == = String.Format (res.getstring (R.Stringescapedusername = html.fromhtml (text);
3.APP Resources-resource types/string Resources