3.APP Resources-resource types/string Resources

Source: Internet
Author: User

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&apos;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$sis 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:

<string name="welcome_messages">hello,%1$s! You have &lt;b>%2new messages&lt;/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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.