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/>}