Android adaptive screen orientation/size/Resolution

Source: Internet
Author: User

Screen Size:

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.

Screen direction:

Automatic switching of landscape and portrait screens:

You can create two layout-port and layout-land directories under the res directory, which are placed in two layout files: vertical screen and horizontal screen, 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.

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
}
}

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/fenghome/archive/2010/07/27/5769391.aspx

 

 

The following are some Unit issues about resolution: Px, dip, DP, and sp.

Px (pixels) pixels-generally, we use hvga to represent 320x480 pixels, which is usually used.

Dip or DP (device independent pixels) device independent pixels-this is related to the device hardware. We recommend this to support WVGA, hvga, and qvga cwj, instead of pixels.

SP (scaled pixels-best for text size) to zoom in pixels-mainly deal with the font size.

The following are not commonly used, and we know that we will not repeat them too much here.

In (INCHES) inches

Mm (millimeters) mm

Pt (points) Point

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.