Android resource usage String

Source: Internet
Author: User
Tags java format

Android Allows defining multiple string resource files in res/values

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="hello">Hello World!</string>    <string name="app_name">App Name</string></resources>

When a resource file is created or modified here, ADT will automatically update R. java and generate a unique identifier, such:

public static final class string {public static final int app_name=0x7f040004;public static final int hello=0x7f040003;}

You can use the resource in the program to identify the string with R. string. hello, and convert it directly to a string using getText (R. string. hello) in the Activity.

Special Format String

Code

<Resources>
<String name = "java_format_string">
Hello % 2 $ s java format string. % 1 $ s again
</String>
<String name = "tagged_string">
Hello <B> <I> Slanted Android </I> </B>, You are bold.
</String>
</Resources>

 

The special format string here refers to the format string with parameters and the string with HTML tags.

Read operations can be as follows:

 

Code

// Read a Java format string
String javaFormatString = activity. getString (R. string. java_format_string );
// Convert the formatted string by passing in arguments
String substitutedString = String. format (javaFormatString, "Hello", "Android ")
// Set the output in a text view
TextView. setText (substitutedString );
 
// Read an html string from the resource and set it in a text view
String htmlTaggedString = activity. getString (R. string. tagged_string );
// Convert it to a text span so that it can be set in a text view
// Android. text. Html class allows painting of "html" strings
// This is strictly an Android class and does not support all html tags
Spanned textSpan = android. text. Html. fromHtml (htmlTaggedString );
// Set it in a text view
TextView. setText (textSpan );

 

 

Supports multiple languages

To allow applications to support multiple language interfaces, you do not need to redefine the interface. You only need to add a Language Resource Directory and place the corresponding string resource file, for example, res/values-en, to indicate an English string, res/values-zh-rCN indicates Simplified Chinese res/values-zh-rTW indicates traditional Chinese.

Go to the "Settings" program and select "region and text". You can select the corresponding language for testing.

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.