This article summarizes the Android screen switching techniques. Share to everyone for your reference, specific as follows:
First, prohibit the screen switching
Android screen switching in the mobile phone development is more common, many software in the development process in order to avoid the switch between the screen and the cause of unnecessary trouble, usually prohibit the switch on the screen, that is, through the androidmanifest.xml in the activity in the Android: Screenorientation the property value to be implemented.
The Android:screenorientation property, he has the following several parameters:
"Unspecified": The default value is determined by the system to determine the direction of the display. The strategy is related to the device, so different devices have different display orientations.
"Landscape": Horizontal screen display (width ratio to long)
"Portrait": Vertical screen display (height is longer than width)
"User": the user's current preferred direction
"Behind": consistent with the direction of the activity under the activity (in the activity stack)
"Sensor": there are physical sensors to decide. If the user rotates the device, the screen will switch at any screen.
"Nosensor": ignores physical sensors so that they do not change as the user rotates the device (except the "unspecified" setting).
such as the following settings
Copy Code code as follows:
android:screenorientation= "Portrait"
Regardless of how the mobile phone changes, the activity that owns this property will be the vertical screen display.
Copy Code code as follows:
android:screenorientation= "Landscape"
Display for horizontal screen.
The above modifications can also be set in Java code by a code similar to the following
Copy Code code as follows:
Setrequestedorientation (Activityinfo.screen_orientation_landscape)
Second, the overall screen switch change layout
If you want to allow the software to switch between the two screens, because the width of the screen will be converted, it may require different layouts. There are two ways to switch layouts:
1 in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout filename unchanged, such as Main.xml. Layout-land is the horizontal screen of the Layout,layout-port is the layout of the vertical screen, the other without the tube, the screen switch when the program for the call activity OnCreate method, thus loading the response layout.
2 If the layout resources are not set as above, you can use the Java code to determine whether the current is a horizontal or vertical screen and then load the corresponding XML layout file. Because when the screen changes to a horizontal screen, the system will call back the current Activity OnCreate method, you can put the following methods in your oncreate to check the current direction, and then let your setcontentview to load different layout XML.
if (This.getresources (). GetConfiguration (). Orientation = = Configuration.orientation_landscape) {
Log.i ("info", "Landscape"); Horizontal screen
}
else if (this.getresources (). GetConfiguration (). Orientation ==configuration.orientation_portrait) {
log.i ("info", "Portrait");//Vertical Screen
}
Third, through the onconfigurationchanged intercept the horizontal screen transformation
In accordance with the operation of the two, the activity every screen switch will be called onpause-> onstop-> ondestory-> Oncreate->onstart->onresume again, This involves the preservation and reading of content and data, otherwise the content before the turn screen disappears. Many times this result is cumbersome, and Android provides the Android:configchanges attribute in manifest so that the activity does not perpetuate the above reconstruction process. If the user is concerned about the switch of the screen, you can get the current onconfigurationchanged parameters in the activity or view Configuration newconfig function. The order of invocation is similar to the delivery order of touch time, but he does not have the concept of consuming events and will call every onconfigurationchanged function sequentially.
It should be noted that the onconfigurationchanged function can only get the parameters after the switch, in the function to get the new layout and control the size of the location information, if you want to handle the size and location information, must be through the message asynchronous or delay call, Here's the code I need to reset the Popupwindow position when the project needs to switch over and over:
@Override
protected void onconfigurationchanged (Configuration newconfig) {
//TODO auto-generated method stub< C3/>super.onconfigurationchanged (newconfig);
View without creating Handler, you can directly invoke post operations
// new Handler (). postdelayed (New Runnable () {/
/ @Override
// Public void Run () {
// updatepopup (); }
// }, (+);
Postdelayed (New Runnable () {
@Override public
void Run () {
updatepopup ();
}
, 500);//try to get the correct position in the Updatepopup function by using the post operation directly
As for the horizontal screen flip, the system how to let the window flip and redraw, I did not specifically to examine the source code, so it is not known, there is understanding can inform the younger brother.
Of course, if you want to completely prohibit the flip, you can set the properties of the android:screenorientation to Nosensor, so you can ignore the gravity of the trouble caused by induction. But it doesn't work on the simulator, it's right on the real machine.
Here to mention a small knowledge, Android Simulator, shortcut key "CTRL+F11/F12" can be implemented to turn the screen
I hope this article will help you with your Android programming.