Android r.string

Source: Internet
Author: User


In the project, as little as possible in the code in Chinese (except annotations), in this, you need to set the string to String.xml

General usage
EXAMPLE 1:
String.xml in configuration: <string name= "Hello" >Hello!</string>
The layout XML is used in the following way: <textview
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"/>
Code is used in the following way: string string = GetString (R.string.hello); /String string = Getresources (). getString (R.string.hello);

Of course, it is also possible to set the string array in String.xml
EXAMPLE 2:
String.xml in configuration: <string-array name= "Planets_array" >
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
Code is used in the following ways: Resources res = getresources ();
String[] Planets = Res.getstringarray (R.array.planets_array);

Note that special characters are used in the configuration file, eg: single quotation mark "\" in English state, newline "\ n", percent "\%", "\@" symbol
EXAMPLE 3:
String.xml in configuration: correct: <string name= "Good_example" > "This ll work" </string>--in double quotes just as String output
<string name= "good_example_2" >this\ ' ll also work</string>--without double quotes, you need to consider the escape character
<string name= "Good_example_3" formatted= "false" >this ' ll also work</string>--no double quotes, set cancel formatting
Error: <string name= "bad_example" >this doesn ' t work</string>
<string name= "bad_example_2" >xml encodings don&apos;t work</string>

Of course, wildcard methods are also supported in String.xml
EXAMPLE 4:
String.xml in configuration: <string name= "Welcome_messages" >hello,%1$s! You have%2$d new messages.</string>
% 1,% 2 represents the order of the parameters, $s represents the output string, $d represents the output decimal integer, $f represents the output decimal floating point number
Code is used in the following ways: Resources res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);

Finally, a string with HTML style
EXAMPLE 5:
String.xml in configuration: <string name= "Welcome_messages" >hello,%1$s! You have &lt;b>%2$d new messages&lt;/b>.</string>
It is important to note that the escape character is used,eg:< or &amp, and example 3 can be referenced.
Code is used in the following ways: Resources res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount);
Charsequence Styledtext = html.fromhtml (text);

It is important to note that the parameter 1:username, if the username contains special characters < or &, you need to escape the special character
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);

According to the official document of the introduction to here, there is a lack of a special character quantity string usage, do not see the formal documentation of the description is less used, ignore it

Android r.string

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.