What do you think about horizontal and vertical screen switching for Android?

Source: Internet
Author: User

Recently, we have used the knowledge of switching between landscape and landscape screens. We all know that after switching between landscape and landscape screens, the activity will re-execute the oncreat function. However, you only need

After Android: screenorientation = "user" Android: configchanges = "orientation | keyboardhidden" is added<Activity Android: Name = ". mainactivity "Android: Label =" @ string/app_name "<br/> Android: screenorientation =" user "Android: configchanges = "orientation | keyboardhidden"> <br/> <intent-filter> <br/> <action Android: Name = "android. intent. action. main "/> <br/> <category Android: Name =" android. intent. category. launcher "/> <br/> </intent-filter> <br/> </activity> 

After switching between the landscape and landscape screens, we will not execute the oncreat function, but will call onconfigurationchanged () so that we can control the switching between landscape and landscape screens.

Of course, you only need to set Android: screenorientation = "Landscape" if you want to keep it as a horizontal screen.

However, if I want it to start a horizontal screen, the horizontal screen will be displayed, and the vertical screen will show the vertical screen, then, the mobile phone cannot switch between the portrait and landscape screens. How can this problem be solved?

First, append Android: screenorientation = "sensor" Android: configchanges = "orientation | keyboardhidden" to mainfest. xml"

These two attributes.

Step 2: Get the screen length and width and compare and set the variables of the screen.

Display display = getwindowmanager (). getdefadisplay display (); <br/> int width = display. getwidth (); <br/> int Height = display. getheight (); <br/> If (width> height) {<br/> orientation = activityinfo. screen_orientation_landscape; <br/>} else {<br/> orientation = activityinfo. screen_orientation_portrait; <br/>} 

Step 3: append this. setrequestedorientation (morientation) to the onconfigurationchanged () function.

@ Override <br/> Public void onconfigurationchanged (configuration newconfig) {<br/> super. onconfigurationchanged (newconfig); <br/> This. setrequestedorientation (morientation); <br/>} 

However, when you switch to another image and return to the original image, it is still horizontal or vertical. How can I bring it back from another screen and re-layout it?

You only need to set it in onresume. However, only one layout screen is supported. The layout corresponding to the horizontal and vertical screens does not know how to solve the problem.

If you have any ideas, leave a message.

@ Override <br/> protected void onresume () {<br/> morientation = activityinfo. screen_orientation_user; <br/> This. setrequestedorientation (morientation); <br/> display = getwindowmanager (). getdefadisplay display (); <br/> int width = display. getwidth (); <br/> int Height = display. getheight (); <br/> If (width> height) {<br/> morientation = activityinfo. screen_orientation_landscape; <br/>} else {<br/> morientation = activityinfo. screen_orientation_portrait; <br/>}</P> <p> super. onresume (); <br/>} 

 

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.