Android adaptive screen orientation and size

Source: Internet
Author: User

I. Different Layout

Android phoneScreenDifferent Sizes, with 480X320,640x360,800 x480. How can I make the app automatically adapt to differentScreenWhat about it?
In fact, it is very simple, just need to create different layout file folders in the res directory, such as layout-640x360, layout-800x480, all layout files will be written into R. Java after compilation, and the system willScreenSelect the appropriate layout for use.

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 platforms during program developmentScreenWe recommend that you store images of different versions in different folders as needed.

ScreenDirection:

Automatic switching of landscape and portrait screens:

You can create the layout-port and layout-land directories under the res directory. The layout files are placed in the portrait and landscape screens respectively.ScreenWhen the direction changes, the system automatically calls the corresponding layout file to avoid the failure of one layout FileScreenIssues displayed.

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"?> <Br/> <manifest xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> package = "com. ray. linkit "<br/> Android: versioncode =" 1 "<br/> Android: versionname =" 1.0 "> <br/> <application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"> <br/> <activity Android: Name = ". main "<br/> Android: Label =" @ string/app_name "<br/> Android: screenorientation = "portrait"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> <br/> <activity Android: Name = ". gameplay "<br/> Android: screenorientation =" portrait "> </activity> <br/> <activity Android: Name = ". optionview "<br/> Android: screenorientation =" portrait "> </activity> <br/> </Application> <br/> <uses-SDK Android: minsdkversion = "3"/> <br/> </manifest>

 

In additionScreenThe switch will restart the activity, so you should save the status of the current activity before the activity is destroyed, and load the configuration when the activity is created again, so that the ongoing 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 <br/> Public void onconfigurationchanged (configuration newconfig) {<br/> super. onconfigurationchanged (newconfig); <br/> If (this. getresources (). getconfiguration (). orientation = configuration. orientation_landscape) {<br/> // land do nothing is OK <br/>} else if (this. getresources (). getconfiguration (). orientation = configuration. orientation_portrait) {<br/> // port do nothing is OK <br/>}< br/>}

 

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.