How to use Android strings.xml and tips _android

Source: Internet
Author: User

I believe Strings.xml is already one of the most familiar files in Android development, but it also has a lot to notice and some tips, knowing that these can make your Android application more standard and easy to use, let's take a look. :   )

Do not reuse

This may be a lot of people have different opinions, because the popular programming philosophy is to teach us to reuse code, of course, code reuse is a good idea, can make the program more concise. But it is also easy to form the thought inertia that everything wants to reuse, and this can cause trouble in some scenarios.

For example, imagine that you are using the same string –r.string.loading in your application's login and registration interface.

<string name= "Loading" >Loading...</string>

Then, if the product requirements change, and you want to use different prompts separately, you will have to create two strings and configure them in your code. So if you're configuring separate pages from the start, all you need to do is modify the Strings.xml file.

<string name= "Sign_in_loading" > is logging in ...</string>
<string name= "Sign_up_loading" > is registering ...</string>

One reason to be more easily overlooked is that if your application does not want to face only domestic users, but also face other language users, some languages may cause unexpected problems.

Because some languages have the same meaning in different contexts, will use different words, the taste of the experience of nature will understand.

Good file structure

<!--register start-->
<string name= "Register_username" > Username </string>
<string name= " Register_password "> Password </string>
<!--register End-->

<!--login start-->
< String name= "Login_username" > Username </string>
<string name= "Login_password" > Password </string>
<!--login End-->

Do not bother with this, use such a way to organize strings.xml files, after the application becomes complex, it is also easy to search and even observe the way to find the string you want to modify.

Formatting

Never use string concatenation, because the sequence of sentences in different languages is diverse, and string concatenation makes your logic very complex.

This is the time to consider the use of string formatting:

<string name= "Welcome_messages" >hello,%1$s! You have%2$d new messages.</string>

%1 $ s represents a formatted string, and%2$d represents the formatted value and in the second position, you can do the same.

Java Code: Resources


res = getresources ();
String Text = String.Format (res.getstring (r.string.welcome_messages), username, mailcount); Note the order of the parameters.

Plural nouns

Do not deal with the plural of words in your Java code as follows, because different languages have different grammatical rules for complex numbers.

<!--strings.xml start-->
<string name= "book" >book</string>
<string name= "Books" > Books</string>
<!--strings.xml end-->

if (Bookcount = = 0) {
  text = getString (R.string.book);
else {
  text = getString (r.string.books);
}

The correct approach should be to use the getquantitystring (int id, int quantity) method.

<plurals name= "book" >
  <item name= "One" >book</item>
  <item name= "Others" >books</ item>
</plurals>

int bookcount = 4;
Resources res = getresources ();
String Bookcount = res.getquantitystring (R.plurals.book, bookcount);
Result:books.

Of course Quantity String supports not only one, but also zero, two, few, many, and other.
It is free to decide what words to use in each case (of course, it may not be very useful in the Chinese context).

And Quantity String can also be used in combination with the format mentioned above:

<plurals name= "book" >
  <item name= "One" >%d book found.</item>
  <item "Name=" > %d books found.</item>
</plurals>

int count = 4;
Resources res = getresources ();
String Bookcount = res.getquantitystring (R.plurals.book, Count, count);
Result:4 Books found.

Google's official recommendation is that, as a developer, it should at least provide ' one ' and ' other ' attributes to nouns.

Text highlighting

You might know that using Foregroundcolorspan or Spannablestringbuilder to highlight something in a piece of text, but that might not be the best way to do it in a multi-language application, Because both methods depend on the exact location of the highlighted text as parameters. If the application needs more languages to support, it will write a lot of Java code and frequently compute the location of the highlighted content.

This is the time to try HTML:

<string name= "Html_text" formatted= "false" >
<![ cdata[    
<font color=\ ' #28b5f5 ' >Hello</font> world.
]] >
</string>

TextView TV = (TextView) Findviewbyid (r.id.tv_txt);
Tv.settext (html.fromhtml (getString (R.string.html_text)));

This is actually a format, we can not only define the color, but also use < b >, < i >, < u > to make the string bold, italic and underline respectively.

In fact, the use of strings.xml is far less than many people think so simple, here is also a brick to welcome jade, if you want to know more detailed usage, you can refer to the official documents. :   )

The above is the use of the Android Strings.xml file methods and skills to do the summary, follow-up continue to organize, thank you for your support for this site!

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.