After switching between the landscape and landscape screens, the activity will re-execute the oncreat function, but Android: screenorientation = "is added to mainfest. xml of the android project"User"Android: configchanges ="Orientation | keyboardhidden"Then, the oncreat function will not be executed after the screen switching, but will call onconfigurationchanged (), so that we can control the switching of the screen.
Or create the layout-land and layout-port directories under the res directory, and the corresponding layout files remain unchanged. Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen.
If you want it to be displayed on a horizontal screen, you only need to set Android: screenorientation = "Landscape" in the configuration file, orCodeSetrequestedorientation (activityinfo. screen_orientation_landscape );. If you want to keep it portrait, you only need to set Android: screenorientation = "portrait" in the configuration file, or in the code.Setrequestedorientation(Activityinfo. screen_orientation_portrait );.
If the screen is a landscape screen at startup, the landscape screen is displayed. If the screen is a landscape screen, the phone cannot switch the landscape screen.
Step 1: append Android: screenorientation = "in mainfest. xml"Sensor"Android: configchanges ="Orientation | keyboardhidden"
Step 2: Get the screen length and width and compare and set the variables of the screen.
Display display = getwindowmanager (). getdefaultdisplay ();
Int width = display. getwidth ();
Int Height = display. getheight ();
If (width> height ){
Orientation = activityinfo. screen_orientation_landscape;
} Else {
Orientation = activityinfo. screen_orientation_portrait;
}
Step 3: append this. setrequestedorientation (morientation) to the onconfigurationchanged () function.
@ Override
Public void onconfigurationchanged (configuration newconfig ){
Super. onconfigurationchanged (newconfig );
This. setrequestedorientation (morientation );
}
When you switch to another image and return to the original image, it is still horizontal or vertical. If you want it to return from another screen, you only need to set it in onresume (), but this only supports layout.