Android horizontal and vertical screen Switching

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 Android: configchanges = "orientation | keyboardhidden" in your specified activity in androidmanifest. xml"

Then, in the logic processing of the activity (CodePart) to reload the onconfigurationchanged event. 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.):

@ Override public void onconfigurationchanged (configuration config) {try {super. onconfigurationchanged (newconfig); If (this. getresources (). getconfiguration (). orientation = configuration. orientation_landscape) {// code to be processed during landscape screen display. // The code here is setrequestedorientation (activityinfo. screen_orientation_portrait);} else if (this. getresources (). getconfiguration (). orientation = configuration. orientation_portrait) {// code 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: 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.