Android screen switching and its corresponding layout loading problem

Source: Internet
Author: User

First, the overall screen switch and the layout problem of the portrait and vertically:

If you want the software to switch between the screens and the screen, it may require a different layout because the aspect of the screen will change.

There are two ways to switch the layout:

1) in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout file name is unchanged, such as: Layout-land is the horizontal screen of the layout,layout-port is vertical screen layout, other without tube, The program calls the activity's OnCreate method in Setoncontent (XXX) and automatically loads the corresponding layout when the screen is switched.

2) If the layout resource is not set as above, then the Java code can be used to determine whether the current horizontal screen or vertical screen and then to load the corresponding XML layout file. Because when the screen changes to a horizontal screen, the system reloads the current activity's OnCreate method (that is, the activity's life cycle will start over), you can put the following methods in your oncreate to check the current direction, You can then have your setcontentview loaded into different layout.

/** 1: Vertical screen    */    Private int orientation;orientation=getresources (). GetConfiguration (). Orientation;

Second, the screen is forced to set the horizontal and vertical screen direction:

Android screen switching in mobile phone development is more common, many software in the development process in order to avoid the vertical and horizontal screen switching caused unnecessary trouble, usually forced to set the direction of the screen,

This is done by setting the value of the Android:screenorientation property in the activity in Androidmanifest.xml.

such as the following settings:

Horizontal Display settings: android:screenorientation= "Lanscape" vertical display settings: android:screenorientation= "Portrait"

Of course, the above changes can also be implemented in Java code through code: (Android screen switch will restart activity, so save the current active state before the activity is destroyed, and load the configuration when the activity again create)

Setrequestedorientation (Activityinfo.screen_orientation_landscape)

Third, intercept the configuration file required for screen switching: onconfigurationchanged

The activity will recall the onpause->onstop-> Ondestory->oncreate->onstart->onresume (for this content and data to be saved and read) each time the screen toggles. Otherwise the content will disappear before the screen is turned on.)

Many times this result makes the program cumbersome, for this reason Android provides the android:configchanges attribute in manifest, so that the activity does not continue the above reconstruction process;

Mode one) in the Android project Mainfest.xml configuration activity:android:configchanges= "Keyboardhidden|orientation , After the screen switch is not going to execute the Oncreat function, but will go to call onconfigurationchanged () so that you can control the screen switch.

Mode two) The user can get the current screen parameters in the activity or view:onconfigurationchanged (configurationnew Config) , function. The sequence of calls is similar to the order in which the touch time is passed, but he has no concept of consuming events and will call each of the onconfigurationchanged functions sequentially.

you need to rewrite the activity's Onconfigurationchanged method. The implementation is as follows, do not need to do too much content :

It is important to note that the onconfigurationchanged function can only get the parameters after the screen switch, in the function to get the new layout and control of the size position information, if you want to handle the size and location information, must be called through the message asynchronous or delay;

@Override Public voidonconfigurationchanged (Configuration newconfig) {Super. onconfigurationchanged (Newconfig); if(Getresources (). GetConfiguration (). Orientation = =Configuration.orientation_landscape) {                        //Land does nothing is OK}Else if(Getresources (). GetConfiguration (). Orientation = =configuration.orientation_portrait) {                        //Port do nothing is OK                }        } 

The Adaptive Switching screen:

If you want it to start when the horizontal screen, the horizontal screen, the vertical screen of the vertical display, and then mobile phone switching screen can not use the How to solve it?

First: Append in Mainfest.xml

Android:screenorientation= "sensor" android:configchanges= "Orientation|keyboardhidden"

Then: Get the length and width of the screen, and compare the variables to set the screen.

Display display = Getwindowmanager (). Getdefaultdisplay ();         int width = display.getwidth ();         int height = display.getheight ();         if (Width > height) {            //  horizontal screen        else  {            //  vertical screen        }

Then: Append this.setrequestedorientation (Orientation) in the onconfigurationchanged () function

 Public void onconfigurationchanged (Configuration newconfig) {                   super. onconfigurationchanged ( Newconfig);                     This . Setrequestedorientation (orientation);               }  

But then you go back to the original picture when you cut it into another picture, and it's still horizontal or vertical. How to get it back from another screen, and then re-screen layout?

As long as in the Onresume () in the setting, but this is only support for the screen only one layout;

protected voidOnresume () {Orientation=Activityinfo.screen_orientation_user;  This. Setrequestedorientation (orientation); Display Display=Getwindowmanager (). Getdefaultdisplay (); intwidth =display.getwidth (); intHeight =display.getheight (); if(Width >height) {Orientation=Activityinfo.screen_orientation_landscape; } Else{Orientation=activityinfo.screen_orientation_portrait; }        Super. Onresume (); }

Please note that there are three points:

1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice
2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once
3, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, will only execute onconfigurationchanged method

Android screen switching and its corresponding layout loading problem

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.