I believe Strings.xml is one of the most familiar files in Android development, but in fact it also has a lot of attention to the place and some tips, know that these can make your Android application more standardized and easy to use, let's take a look at it. : )
No re-use
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's also easy to form the inertia of thinking that you want to reuse, which can cause trouble in some scenarios.
For example, imagine that you are using the same string –r.string.loading in the login and registration interface in your app.
<name="Loading" >loading ... </string>
Then, if the product requirements change and you use different prompts, you will have to create two new strings and configure them in your code. So if you are configuring different pages from the beginning, it is only necessary to modify the Strings.xml file.
<name="sign_in_loading" > Logging in ... </string><name="sign_up_loading" > Registering ... </string>
One reason to be more easily overlooked is that if your application does not want to face only domestic users and other language users, some languages may cause unexpected problems.
Because some languages the same meaning in different contexts, will use different words, the taste of the experience of nature will understand.
Good file structure
<!--register Start-to-<StringName="Register_username" > User name</string>< string name= "Register_password" > Password </string><!--Register End-- ><!--login start-->< String name= "login_username" > User name </string><string name= "Login_password" > Password </string><!--login end-->
Don't be bothered about this, organize your strings.xml files in such a way that you can easily find the strings you want to modify by searching and even observing in a way that makes the application complex.
Formatting
Never use string concatenation because the order of sentences in different languages is varied, and string concatenation makes your logic very complex.
This is the time to consider using string formatting:
<string name="Welcome_messages" >hello,%1$s! You have%2$d new messages.</string>
%1 $ s represents a formatted string,%2$D represents a formatted value and in the second position, you can do so.
Java code: Resources res = getresources (); String.Format (res.getstring (r.string.welcome_messages), username, mailcount); //Note the order of the parameters.
Plural nouns
do not handle word complex problems in your Java code as follows, because different languages have different grammatical rules for complex numbers.
<!--strings.xml Start--<name= "book" >book</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 not only supports one, but also zero, one, few, many and other.
You are free to decide what words to use in each case (of course, it may not be very useful in a Chinese environment).
And Quantity String can be used in conjunction with the format mentioned above:
< plurals name= "book" > <item name= "one" >%d book Found. </item> <item name= "others" >%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 proposal that as a developer should at least give the noun the ' one ' and ' other ' attributes.
Text highlighting
You may know that you can highlight something in a text with Foregroundcolorspan or spannablestringbuilder, but this may not be the best way for a multilingual app, Because both methods rely on the exact location of the highlighted text as the parameter. If the application needs to support more languages, this will write a lot of Java code, and frequently calculate the location of the content to be highlighted.
You can try the HTML at this time:
<name=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 also a format, we can not only define the color, you can also use < b >, < i >, < u > to make the string bold , italic and underline, respectively.
In fact, the use of strings.xml far less than many people think so simple, here is also a throw brick welcome jade, if you want to know more detailed usage, you can refer to the official documents. : )
"Reprint" The only thing that's known about Android Strings.xml