[Based Android] makes it easy to use multiple languages-how to use the best resources for Android

Source: Internet
Author: User

Unlike the actual product development, when you write an Android app and play it on your own, you don't need to worry about a problem, that is, you can configure different resources for mobile phones with different configurations.

This problem is fixed today. That is to say, due to the increasing number of devices and versions of Android, Android supports providing different resources for different device environments. In fact, I feel that this issue will become more and more worthy of the attention of Android Developers, because the current system version is faster and Android 5.0 is expected to be released this fall, which will be a new SDK, in addition, manufacturers such as HTC Samsung and Sony are constantly releasing new resolution and size mobile phones, the change will be even greater. The language version of the app is complicated because it is internationalized... All determine the occurrence of this problem ..

In fact, things are far from so complex, developmentProgramIt's still not that difficult. Today, I just suddenly think of it, Google, and nothing like this.Article... So let's just translate how Android selects resources under different resource folders to access.

The following is an illustration on the android Official Website:

This is how Android selects resources under the res folder based on the actual situation of the local machine. (For details about the resource types under Res, refer to the official documentation ), sometimes Several folders can meet the configuration requirements, but only one folder will be selected.

1. First, the system will exclude the delimiters that do not meet the actual conditions. For example, if the current mobile phone language environment is Chinese Zn, it is similar

    • Drawable-en-Port/
    • Drawable-en-notouch-12key/
Such folder resources will be excluded. 2. The system will refer to a table containing all the configurations, and the weights will decrease from top to bottom. For example, screen orientation is before screen pixel density (DPI, therefore, the first screen orientation parameter is preferred. At this time, it will check all the folders. If the current configuration is a land-scape landscape screen, it will find whether there are folders containing-land, if so, all other folders that do not contain land are excluded. If not, compare the following priorities, and so on until only one folder is left. What if there is no full file store ?.... Obviously, crash is lost. Therefore, the official recommendation is provided. Make sure that there is a default folder to handle the general situation, such as drawable. This will increase the robustness of the program. Basically, this is just a summary of small issues. I believe that the experienced project experts have long been familiar with it. The following is an example to enable your application to support multiple languages ~~~ We simply write a prompt to input the view. The following is main. xml.
 1   <?  XML version = "1.0" encoding = "UTF-8"  ?> 
2 < Linearlayout Xmlns: Android = "Http://schemas.android.com/apk/res/android"
3 Android: layout_width = "Fill_parent"
4 Android: layout_height = "Fill_parent"
5 Android: Orientation = "Vertical" >
6
7 < Textview
8 Android: layout_width = "Fill_parent"
9 Android: layout_height = "Wrap_content"
10 Android: Text = "@ String/hello"
11 Android: textsize = "24sp"
12 Android: textstyle = "Bold" />
13
14 < Edittext
15 Android: ID = "@ + ID/mytext"
16 Android: layout_width = "Fill_parent"
17 Android: layout_height = "@ Dimen/basic_button"
18 />
19 </ Linearlayout >

When displaying text, we define a string resource. Some friends who write the android program for the first time will think that there is no need to reference resources, but will directly write the required text here in text, such

Android: text = "Hello world, mutiversionactivity! "
 
 
 
However, when we need to create multiple language versions, it will be inconvenient. Do we have to write two views?
 
In addition to this problem, the above method of referencing resources does not conform to an important idea of Android program design, that is, the reference of resources, and the iPhone is also such a design idea.
 
Now, Android will select different configuration files based on different configuration environments.
 
For the convenience of the experiment, suppose we need to display the English interface when the device is portrait, and the Chinese interface when the device is landscape.
 
Therefore, we create the following resource folders for two different configurations:

The strings. XML in values (for the default resource folder) is

 1   <?  XML version = "1.0" encoding = "UTF-8"  ?> 
2 < Resources >
3
4 < String Name = "Hello" > Hello world, mutiversionactivity! </ String >
5 < String Name = "App_name" > Mutiversion </ String >
6
7 </ Resources >

Strings. XML in values-land (resource folder for landscape) is

 1   <?  XML version = "1.0" encoding = "UTF-8" ?> 
2 < Resources >
3
4 < String Name = "Hello" > This is a Chinese horizontal screen interface </ String >
5 < String Name = "App_name" > Multi-version Test </ String >
6
7 </ Resources >

In this way, when we reference character resources directly in the following way, the system will automatically select resources that meet the requirements.

 
Android: text = "@ string/hello"
 
The following is a running command, which can be switched in real time... You can give it a try.

 

Switch to the Chinese UI when you move the phone horizontally. The test environment is HTC Desire HD Android 2.2.

 

 

Of course, android can dynamically change resources based on different configurations. In fact, when orietation changes, the activity goes through a new declaration cycle, and

Handling runtime Changes Mechanism is related. Later, I think it is necessary to discuss it in depth, because when I set in the program that does not allow the system to automatically save the status, the Chinese and English in the example are invalid.

This article is written on and off, so the idea is a bit messy. Whatever the case, people who want to be able to read it can understand it.

 

 

 

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.