Android adaptive screen size and layout (landscape screen | landscape screen)

Source: Internet
Author: User

I. Different Layout

The screen sizes of Android mobile phones vary by 480X320,640x360,800 X. How can an app automatically adapt to different screens?
In fact, it is very simple, just need to create different layout folder under the res directory, such as layout-640x360, layout-800x480, all the layout files will be written to R after compilation. java, and the system selects the appropriate layout based on the screen size.
Ii. hdpi, mdpi, and ldpi
In earlier versions, there was only one drawable, while in version 2.1, there were three types: drawable-mdpi, drawable-ldpi, and drawable-hdpi, which were mainly used to support multi-resolution.
Differences between drawable-hdpi, drawable-mdpi, and drawable-ldpi:
(1) The drawable-hdpi contains high-resolution images, such as WVGA (480x800) and fwvga (480X854)
(2) drawable-mdpi stores medium-resolution images, such as hvga (320x480)
(3) drawable-ldpi stores low-resolution images, such as qvga (240x320)
The system will find the corresponding images in these folders according to the machine resolution.
To be compatible with different screens on different platforms, we recommend that you store images of different versions in different folders as needed.
[I] Note: the resolution of the three items is different. Just as you lower the resolution of your computer, the image will become larger. On the contrary, the resolution will be high and the image will be reduced.[/I] 
Screen direction:
Automatic switching of landscape and portrait screens:
You can create two directories: layout-port-800x600 and layout-land under the res directory, which are placed in the vertical and horizontal layout files, in this way, the system automatically calls the corresponding layout file when the screen direction of the mobile phone changes, to avoid the problem that one layout file cannot meet the two screen display conditions.
Automatic switching of landscape and portrait screens with different resolutions:
Take 800x600 as an Example
Two directories of layout-port-800x600 and layout-land-800x600 can be created under the res directory
Do not switch:
The following steps are circulated on the Internet, but I used to implement this configuration through a graphical interface, which is the same in different ways. I will paste the image when I have time.
Note: Each activity has this screenorientation attribute. You must set this attribute for each activity. You can set this attribute to a portrait screen (portrait) or a non-gravity sensor (nosensor ).
Solution to keep the program interface in one direction and not change with the orientation of the mobile phone:
Configure androidmanifest. xml. Add this line to Android: screenorientation = "Landscape ".
For example, (landscape is horizontal and portrait is vertical ):
Java code:
<? XML version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
Package = "com. Ray. linkit"
Android: versioncode = "1"
Android: versionname = "1.0" type = "codeph" text = "/codeph">
<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name">
<Activity Android: Name = ". Main"
Android: Label = "@ string/app_name"
Android: screenorientation = "portrait">
<Intent-filter>
<Action Android: Name = "android. Intent. Action. Main"/>
<Category Android: Name = "android. Intent. Category. launcher"/>
</Intent-filter>
</Activity>
<Activity Android: Name = ". gameplay"
Android: screenorientation = "portrait"> </activity>
<Activity Android: Name = ". optionview"
Android: screenorientation = "portrait"> </activity>
</Application>
<Uses-SDK Android: minsdkversion = "3"/>
</Manifest>

In addition, the activity is restarted every time the screen is switched on in Android. Therefore, you should save the status of the current activity before the activity is destroyed and load the configuration when the activity is created again, in progress, the game will not automatically restart!
Some programs are suitable for switching from a portrait screen to a landscape screen, or vice versa. What should we do at this time? You can configure Android: screenorientation = "portrait" in the activity configuration area ". In this way, you can ensure that the portrait screen is always portrait or landscape.
Some programs are suitable for switching between landscape and landscape screens. What should we do? First, you must configure the activity as follows: Android: configchanges = "keyboardhidden | orientation". In addition, you must override the onconfigurationchanged method of the activity. The implementation method is as follows:
@ Override
Public void onconfigurationchanged (configuration newconfig ){
Super. onconfigurationchanged (newconfig );
If (this. getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){
// Land do nothing is OK
} Else if (this. getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){
// Port do nothing is OK
}
}

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.