Android string, stringarrays, plurals, and formatting and styling

Source: Internet
Author: User

The following is your understanding:

 String,String-array,
Plurals

Both can be defined in: Res/values/strings. xml:

ForString,String-arrayIt may be quite clear that the comparison is mostly used, because the direct Alt +/
There is a prompt for the quick key method, and then you can see these labels

It's easy to use. After defining/strings. xml:, you can find your definition through R. strinb. xxxx.

<? XML version = "1.0" encoding = "UTF-8"?> <Resources>

<String name = "hello"> hello! </String> </resources>

Android has been around for a long time, but it has not become a habit of carefully reading the androidsdk documentation. It is always used to find out where it is used. So today, the decompilation of other people's code has seen

The plurals label does not seem to have been used. You can directly search for it in the SDK and find a very useful tag. In fact, Android provides a lot of convenient notes and optimization solutions, the key is whether you have checked it carefully.

His official documents:

Nonsense not much said: SDK Introduction: http://www.gfan.com/dev/android/guide/topics/resources/string-resource.html

<Textview

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: text = "@ string/Hello"/> the code in the program can get string = getstring (R. String. Hello); this is string-array

<? 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> program code: Resources res = getresources (); string [] planets = res. getstringarray (R. array. planets_array); a pair of plurals strings correspond to a few different words or phrases. In your application, you can use a string pair to reference it. When you request to obtain plurals (plural) you can use getquantitystring () for a resource, but you can specify the Count parameter to obtain the parameter corresponding to the defined plural.
To put it simply, you can use a string to correspond to multiple phrases, but you can use the badge to determine whether you finally correspond to one phrase, which is similar to the switch. <? XML version = "1.0" encoding = "UTF-8"?>

<Resources>

<Plurals name = "numberofsongsavailable">

<Item quantity = "one"> one song found. </item>

<Item quantity = "other"> % d songs found. </item>

</Plurals>

</Resources> JAVA code int COUNT = getnumberofsongsavailable ();

Resources res = getresources ();

String songsfound = res. getquantitystring (R. plurals. numberofsongsavailable, count );

Note: quantity can only take one, zero, and the Other enumeration is limited.

 

Formatting and styling

 

<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 arguements from your application like this:

 

Resources res = getresources ();

String text = string. Format (res. getstring (R. String. welcome_messages), username, mailcount );

So you should understand

Well, I hope it will help you: reprinted, please indicate the difference!

 

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.