Use of horizontal and vertical screens, resources, and internationalization in Android

Source: Internet
Author: User

For Android resources and internationalization problems, we usually create an Android project. The directory structure is shown in:

 
Let's take a look at the layout and values directories. The xml files in layout are the layout files used by our applications, and the sring in values. xml is used to store text resources. A key corresponds to a value.
However, in actual application development, landscape screens (land) and vertical screens (ports) may have different layout files, at this time, we can define the layout file for the horizontal and vertical screens (the file name should be the same). By default, the layout file in the layout directory is loaded. The app also supports different languages. If the app does not define the language resources used by the mobile phone, the value of values is loaded by default.
To help you understand the following simple Demo, take the following steps:
Step 1: Create an Android project named ResourceDemo.
Let's take a look at the automatically generated main. xml layout file in the layout directory. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</LinearLayout>
The text displayed by our program is the hello value in the values/string. xml file. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, ResourceDemo! </String>
<String name = "app_name"> ResourceDemo </string>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, ResourceDemo! </String>
<String name = "app_name"> ResourceDemo </string>
</Resources>
 
Shows the running effect:
Port (portrait mode) effect:

Ctrl + F11 keyboard shortcut simulator to landscape screen (land) mode:

Step 2: Define the layout file of land and port mode, that is, create the layout-land and layout-port folders under the res/directory. The directory results are as follows:

In the layout-land directory, the content of main. xml is basically the same as that of layout, but the content is displayed differently. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/land"
/>
</LinearLayout>
Similarly, the main. xml code in the layou-port directory is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/port"
/>
</LinearLayout>
Of course, the content we display is defined in the values/string. xml file. Here we add two values. The Code is as follows:
View plaincopy to clipboardprint?
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, ResourceDemo! </String>
<String name = "app_name"> ResourceDemo </string>
<String name = "land"> This is land mode. </string>
<String name = "port"> This is port mode. </string>
</Resources>
Run the program again. The effect is as follows:
Port effect:

Horizontal screen (land:

Through the above example, we can conclude that if we have defined the layout files of the horizontal and vertical screens, the layout files with the same name in layout will not be loaded.
Next, let's talk about internationalization. Generally, to internationalization, we only need to redefine the values-country number in the res/directory, such as values-zh-rCN Simplified Chinese, values-zh-rTW traditional Chinese, values-jp.
Shows the directory structure:

Here I only made changes in values-zh-rCN. The Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello World, ResourceDemo! </String>
<String name = "app_name"> ResourceDemo </string>
<String name = "land"> This is the Landscape mode. </string>
<String name = "port"> This is the portrait mode. </string>
</Resources>
Before running the program, change the mobile phone language to Chinese. In the settings-> language & keyboards (language and keyboard) directory, select Simplified Chinese, as shown in:

The effect of running the above project is as follows:

At this time, the display content of our application is Chinese, rather than the content in values/strings. xml.

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.