There are many things to pay attention to and some tips. Knowing these can make your Android application more standardized and easy to use. Interested friends can refer to it.
Don't reuse
Many people may have different opinions on this, because the popular programming concept teaches us to reuse code. Of course, code reuse is a good concept, which can make the program more concise. But it is also easy to form a thinking inertia that you want to reuse everything, which may cause trouble in some scenarios.
For example, imagine that you use the same string in the login and registration interfaces in the application - .
<string name="loading">Loading...</string>
If the product requirements change later, different prompts are used separately, you have to create two new strings and configure them in the code. So if you configure different pages separately from the beginning, all you need to do is to modify the file.
<string name="sign_in_loading">Logining...</string>
<string name="sign_up_loading">Registering...</string>
One reason that is easier to ignore is that if your application does not want to only face domestic users, but also users from other language departments, some languages may cause unexpected problems.
Because some languages use different words in different contexts, and they will naturally understand the taste after experiencing it.
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 -->
Don't think it's troublesome. Use this method to organize files. After the application becomes complicated, you can easily find the string you want to modify by searching or even observing.
format
Never use string splicing method, because the sentence orders of different languages are diverse, and string splicing will make your logic very complicated.
At this time, you can consider using string formatting:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
%1$s represents a string that can be formatted, %2$d represents a value that can be formatted and is in the second position, and so on.
// Java code:Resources res = getResources(); String text = ((.welcome_messages), username, mailCount); // Pay attention to the order of parameters.Plural noun
Don't deal with plural word issues in your Java code like the following, because different languages will have different syntax rules for plural.
<!-- start --> <string name="book">book</string> <string name="books">books</string> <!-- end --> if (bookCount == 0) { text = getString(); } else { text = getString(); }
The correct way to do this 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 = (, bookCount); // result: books.
Of course Quantity String not only supports one, but also zero, two, few, many and other.
You can freely decide which words to use in their respective situations (of course, it may not be of great use in a Chinese environment).
And Quantity String can also be used with the formatting 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 = (, count, count); // result: 4 books found.
Google officially recommends that as a developer, at least the 'one' and 'other' attributes should be provided to the noun.
Highlight the text
You may know that using ForegroundColorSpan or SpannableStringBuilder can highlight certain content in a piece of text, but this may not be the best way for multilingual applications, because both methods rely on the specific position of the text to be highlighted as parameters. If the application needs to support more languages, it will write a lot of Java code and frequently calculate the location of the content to be highlighted.
At this time, you can try HTML:
<string name="html_text" formatted="false"> <![CDATA[ <font color=\'#28b5f5\'>Hello</font> world. ]]> </string> TextView tv = (TextView) findViewById(.tv_txt); ((getString(.html_text)));
This is actually a form of formatting. We can not only define colors, but also use < b >, < i >, and < u > to make the string bold, italic and underscore respectively.
In fact, the usage is far less simple than many people think. Here is a throw-in and welcome. If you want to know more detailed usage, you can refer to the official document.
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.