Android Horizontal screen vertical screen switching problem
I. Prohibit the conversion of the screen
Android screen switch in the development of mobile phones are more common, many software in the development process in order to avoid the screen switching between the need to cause unnecessary trouble, usually prohibit the switch between the screen,
This is done by setting the value of the Android:screenorientation property in the activity in Androidmanifest.xml.
such as the following settings
android:screenorientation= "Portrait"
Regardless of how the phone changes, the activity with this attribute will be displayed on the vertical screen.
android:screenorientation= "Landscape" for horizontal screen display.
The above modifications can also be set in Java code by code similar to the following
Setrequestedorientation (Activityinfo.screen_orientation_landscape)
In addition, the switch of each screen in Android will restart activity, so you should save the current active state before the activity is destroyed, and load the configuration when the activity is re-create, so the game in progress will not be restarted automatically!
Second, the screen switch
If you want the software to switch between the screens and the screen, it may require a different layout because the aspect of the screen will change. There are two ways to switch the layout:
1) in the Res directory to establish the Layout-land and Layout-port directory, the corresponding layout file name is unchanged, such as Main.xml. Layout-land is a horizontal screen of the Layout,layout-port is the layout of the vertical screen, the other without the tube, the screen to switch between the program for the call activity OnCreate method, thereby loading the corresponding layout.
2) If the layout resource is not set as above, then the Java code can be used to determine whether the current horizontal screen or vertical screen and then to load the corresponding XML layout file. Because when the screen changes to a horizontal screen, the system will re-call the current activity's OnCreate method, you can put the following methods in your oncreate to check the current direction, and then you can let your setcontentview to load different layout XML.
if (This.getresources (). GetConfiguration (). Orientation==configuration.orientation_landscape)
{
LOG.I ("info", "landscape"); Horizontal screen
} elseif (This.getresources (). GetConfiguration (). orientation==configuration.orientation_portrait)
{
LOG.I ("Info", "Portrait"); Vertical screen
}
Third, through the onconfigurationchanged intercept the screen transformation
In accordance with the operation of the two, the activity each screen switch will recall the onpause->onstop-> Ondestory->oncreate->onstart->onresume, This involves saving and reading content and data, otherwise the content before the screen disappears. Many times this result makes the program cumbersome, so Android provides the android:configchanges attribute in manifest, so that the activity does not continue the reconstruction process described above. In Android Project Mainfest.xml configuration activity:android:configchanges= "Keyboardhidden|orientation", after the screen switch will not go to execute oncreat function, Instead, it calls onconfigurationchanged () so that it can control the switching of the screen. The user can get the current and the screen parameters in the activity or view onconfigurationchanged (configurationnewconfig) function. The sequence of calls is similar to the order in which the touch time is passed, but he has no concept of consuming events and will call each of the onconfigurationchanged functions sequentially.
You need to rewrite the activity's Onconfigurationchanged method. The implementation is as follows, do not need to do too much content:
@Override
Public voidonconfigurationchanged (Configuration newconfig) {
Super.onconfigurationchanged (Newconfig);
if (This.getresources (). GetConfiguration (). Orientation ==configuration.orientation_landscape) {
Land donothing is ok
} else if (This.getresources (). GetConfiguration (). Orientation ==configuration.orientation_portrait) {
Port donothing is OK
}
}
It is important to note that the onconfigurationchanged function can only get the parameters after the screen switch, in which the new layout and the control's dimension position information is not obtained, and if the dimension and position information is to be processed, it must be called asynchronously or by delay.
Iv. outright prohibition of rollover
Of course, if you want to completely prohibit flipping, you can set the properties of the android:screenorientation to Nosensor, so you can ignore the gravity induction caused by the trouble. However, it does not work on the simulator and is correct on the real machine.
Here is a little knowledge, the Android Simulator, the shortcut key "Ctrl+f11/f12" can realize the rotary screen
Five, adaptive conversion
If you want it to start when the horizontal screen, the horizontal screen, the vertical screen of the vertical display, and then mobile phone switching screen can not use the How to solve it?
First: Append in Mainfest.xml
Android:screenorientation= "sensor" android:configchanges= "Orientation|keyboardhidden"
These two properties.
The second step: Get the length and width of the screen, and compare the variables to set the screen.
1. Display display = Getwindowmanager (). Getdefaultdisplay ();
2. int width = display.getwidth ();
3. int height = display.getheight ();
4. if (width > height) {
5. Orientation = Activityinfo.screen_orientation_landscape; Horizontal screen
6.} else {
7. Orientation = activityinfo.screen_orientation_portrait; Vertical screen
8.}
Step Three: Append this.setrequestedorientation (orientation) to the onconfigurationchanged () function.
1. public void onconfigurationchanged (Configuration newconfig) {
2. super.onconfigurationchanged (Newconfig);
3. This.setrequestedorientation (orientation);
4.}
But then you go back to the original picture when you cut it into another picture, and it's still horizontal or vertical. How to get it back from another screen, and then re-screen layout?
Just set it in Onresume (). But this only supports only one layout of the screen. The layout of the screen is not sure how to solve.
1. protected void Onresume () {
2. Orientation = Activityinfo.screen_orientation_user;
3. This.setrequestedorientation (orientation);
4. Display display = Getwindowmanager (). Getdefaultdisplay ();
5. int width = display.getwidth ();
6. int height = display.getheight ();
7. if (Width > height) {
8. Orientation = Activityinfo.screen_orientation_landscape;
9.} else {
Ten. Orientation = activityinfo.screen_orientation_portrait;
11.}
Super.onresume ();
13.}
Vi. Summary
In short, for the overall screen switching problem, statistics, the solution is:
① ignored.
② only Vertical display (android:screenorientation= "Portrait")
Horizontal screen display only (android:screenorientation= "landscape")
③ simple to prevent overloading:
Added in Androidmanifest.xml: android:configchanges= "Orientation|keyboardhidden"
Overloading the Onconfigurationchanged event in activity
@Override
Publicvoid onconfigurationchanged (Configuration config) {
super.onconfigurationchanged (config);
}
④ layout of each screen
The layout of the screen is as follows:
New in Res
Layout-land Horizontal Screen
Layout-port Vertical Screen
And then the layout of the XML files to the above directory, modify layouts can be in the code to make no changes.
In the main activity in the Androidmanifest.xml file, add
Android:configchanges= "Orientation|keyboardhidden"
Then join the onconfigurationchanged in the main activity
@Override
public void onconfigurationchanged (Configuration config) {
super.onconfigurationchanged (config);
if (config.orientation = = configuration.orientation_portrait) {
Setcontentview (R.layout.main); Layout
TV = (TextView) Findviewbyid (r.id.edittext01);//control
}
if (config.orientation = = Configuration.orientation_landscape) {
Setcontentview (R.layout.main); Layout
TV = (TextView) Findviewbyid (r.id.edittext01);//control
}
}
Vii. example Detailed steps
------First step: Get permission
Need to add the appropriate license in
------Step Two: Set up for activity based on different goals
Target 1: Toggle the screen
Step: Set A default screen orientation method for activity as follows:
Find the activity in Androidmanifest.xml to add code:
Android:name= ". Activityname "
android:screenorientation= "Landscape"
>
Set the activity's default orientation to "landscape"
The screenorientation here have the following options:
==================================================================
= Unspecified default value, automatically switched by system-determined state
= Landscape Horizontal Screen
= Portrait Vertical Screen
=user user's current set orientation value
= behind the orientation value of the activity to be displayed next
= Sensor sensor's direction of use
= Nosensor not using the sensor is basically equivalent to unspecified
==================================================================
Goal 2: Prevent the destruction of activity
Step: Set the Configchanges property for activity
Find the activity in Androidmanifest.xml to add code:
Android:name= ". Activityname "
Android:configchanges= "Orientation|keyboardhidden"
>
The configchanges here have the following options:
==================================================================
= Orientation screen rotates between portrait and landscape
= Keyboardhidden keyboard to show or hide
= Fontscale user changed the preferred font size
= Locale user has selected different language settings
= Keyboard keyboard type change, e.g. mobile phone switch from 12 keyboard to full keyboard
= touchscreen or navigation keyboard or navigation change, typically this event does not occur
==================================================================
If you need more than one option, use the ' | ' Separated
Note here: If you are testing on a physical machine, the orientation option is required for the screen shift
"Focus" If you want to make the program can be tested on the Android emulator need to write Orientation|keyboardhidden
If the missing Keyboardhidden option does not prevent the destruction of the activity
And in the later mentioned Onconfigurationchanged event can only capture vertical screen of the event can not capture the horizontal screen vertical screen
Target 3: Capture the events of the portrait and landscape toggle
Step: Rewrite the onconfigurationchanged event in activity (Activityname.java)
@Override
public void onconfigurationchanged (Configuration newconfig) {
TODO auto-generated Method Stub
Super.onconfigurationchanged (Newconfig);
Switch (newconfig.orientation)
{
Change to Landscape
Case (Configuration.orientation_landscape):
If you have something to do when converting to a landscape screen, please write it here
Break
Change to Portrait
Case (configuration.orientation_portrait):
If you have something to do when converting to a vertical screen, please write it here
Break
}
}
Eight, remark:
1, do not set the activity of the android:configchanges, the screen will recall the various life cycle, cut across the screen will be executed once, cut the vertical screen will be executed twice
2, set the activity android:configchanges= "orientation", the screen will recall the various life cycle, cut horizontal, vertical screen will only be executed once
3, set the activity android:configchanges= "Orientation|keyboardhidden", the screen will not recall the various life cycle, will only execute onconfigurationchanged method
Android Horizontal screen vertical screen switching problem