Summary of Android horizontal and vertical screen Switching

Source: Internet
Author: User

Android horizontal and vertical screens have two problems to solve:

I. layout problems

Ii. Reload

1. layout Problem: if you do not want to allow the software to switch between the horizontal and vertical screens, the simplest way is in the androidmanifest of the project. in XML, add the Android: screenorientation attribute to the activity you specified. It has the following parameters:

"Unspecified": The default value is used by the system to determine the display direction. The determined policy is related to the device, so different devices have different display directions.
"Landscape": Landscape Display (longer than width/height)
"Portrait": Portrait display (height to width)
"User": the user's current preferred direction
"Behind": the direction of the activity under the activity is the same (in the activity stack)
"Sensor": determined by physical sensors. If you rotate the device, the screen will be switched horizontally and vertically.
"Nosensor": Ignore the physical sensor so that it will not be changed as the user rotates the device (except for the "unspecified" setting ).

You can alsoCodeSetrequestedorientation (activityinfo. screen_orientation_landscape.

If you want the software to switch between portrait and portrait screens, the height and width of the portrait screen may be converted, and different la s may be required. You can switch the layout using the following methods:

1) Create the layout-land and layout-port directories under the res directory. The corresponding layout files remain unchanged, such as main. xml. Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen. If you do not need to worry about it, the simulator will automatically search for it.

2) This. getresources (). getconfiguration (). orientation is used to determine whether the current screen is a horizontal screen or a vertical screen and then load the corresponding XML layout file. When the screen changes to a horizontal screen, the system will call the oncreate method of the current activity again. You can put the following method in your oncreate to check the current direction, then, let your setcontentview load different layout XML.

If ( This . Getresources (). getconfiguration (). Orientation = configuration. orientation_landscape ){
Log. I ("info", "Landscape "); // Landscape Screen
}
Else If ( This . Getresources (). getconfiguration (). Orientation = configuration. orientation_portrait ){
Log. I ("info", "portrait "); // Portrait Screen
} The onconfigurationchanged () method can also detect the keyboard status with a hard keyboard. // Detect the status of the physical keyboard: Push or close
If (Newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_no ){
// The physical keyboard is in the release status, where additional processing code is added
}
Else If (Newconfig. hardkeyboardhidden = configuration. hardkeyboardhidden_yes ){
// The physical keyboard is closed and additional processing code is added here
}

2. Reload. If you do not need to load the data again, you can go to androidmanifest. add Android: configchanges = "orientation | keyboardhidden" to XML. The function of configuring Android: configchanges is as described in the document: specify one or more configuration changesthat the activity will handle itself. if not specified, the activity will berestarted if any of these configuration changes happen in the system. In this wayProgramThe activity in does not repeatedly call oncreate () or even does not call onpause or onresume. Only one onconfigurationchanged (configuration newconfig) is called ). If you need to reload data, you do not need to make any modifications. However, if you need to save the previous operation content or data during the reload process, you need to save the previous data. Then it is obtained in oncreate () of the activity. Of course, you cannot set Android: configchanges (). Otherwise, the oncreate () method will not be called.

If you want to completely disable flip, you can set the attribute of Android: screenorientation to nosensor, so you can ignore the troubles caused by gravity sensing. However, it is correct on a real machine that does not work on the simulator. Android: screenorientation = "portrait"

No matter how the phone changes, the activity with this attribute will be displayed in a vertical screen.

Android: screenorientation = "Landscape", which is displayed on a horizontal screen.

Here is a little bit of knowledge. In the android simulator, the shortcut key "Ctrl + F11/F12" can be used to achieve screen conversion.

URL:Http://greatverve.cnblogs.com/archive/2012/02/02/android-Horizontal-vertical.html

 

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.