Android horizontal and vertical screen summary)

Source: Internet
Author: User

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.

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.