First, androidmanifest.xml configuration
1. Add the android:screenorientation= "Landscape" attribute to the Androidmanifest.xml activity (activity required to disable steering) (Landscape is landscape, Portrait is Portrait)
Or
2. by calling Setrequestedorientation (Activityinfo.screen_orientation_landscape) in the inside, you can set the horizontal screen, the same, you can also set up a vertical screen, this needs to see your needs , like me, needs to be the same direction as the previous activity, you can pass a value in the intent, indicating whether it needs to be horizontal or vertical.
Here, you end up coming through this this.getresources (). GetConfiguration (). Orientation to get the current page orientation, you will find the same as the requirements.
Second, the layout file
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 2: Horizontal screen judging the screen in the direction of rotation */ 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;
@Overridepublic void onconfigurationchanged (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 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 Landscape layout File Settings