Resolve the problem of horizontal and vertical screen switching for Android

Source: Internet
Author: User

During android development, the onConfigurationChanged event is triggered when the screen changes from Portrait evaluation to landscape evaluation by default. By default, the screen is reloaded and the screen is the same as the landscape evaluation, there will be two problems,
Layout problems, the layout displayed on the portrait screen is changed to the landscape screen because of the width and height. All the changes will affect the layout (unless you create two sets of scenes and then add a judge how to display them on the Landscape screen, how to display the image on the portrait screen), of course, the simplest way is to use the AndroidManifest in the project. find the activity you specified in xml and add it
If only the portrait screen is displayed (android: screenOrientation = "portrait ")
For horizontal screen display only (android: screenOrientation = "landscape ")
Picture re-loading problem: by default, the screen will be re-loaded during horizontal and vertical switching, leading to unnecessary resource waste, and more serious is the data maintained on the screen (especially for Games) it also has been reset (of course you can save the data to the database or to a text file before the reset), how to avoid re-loading the screen during horizontal/vertical switching,
First, you must find in AndroidManifest. xml and addAndroid: configChanges = "orientation | keyboardHidden"
Reload the onConfigurationChanged event in the logic processing of the activity (in the Code Section). For more information about the code, see ( Note: The following code does not change the screen when switching between landscape and portrait screens, and is processed as portrait screens.): Copy codeThe Code is as follows: @ Override
Public void onConfigurationChanged (Configuration config ){
Try {
Super. onConfigurationChanged (newConfig );
If (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE ){
// The code to be processed when the screen is landscape,
// The code here is used as a portrait screen when the screen is landscape
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );
} Else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT ){
// Code to be processed during portrait
SetRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT );

}
} Catch (Exception ex ){
}
}

You can simply control the display mode of your app screen and the screen will not be reloaded when the display mode remains unchanged, that is, your app is optimized and the user experience is improved accordingly.
We hope to help you. Haha.

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.