Android Screen Toggle Summary (Android data)
Android screen to solve the problem should be two:
I. Layout issues Two. Reload the problem
1. Layout problem: If you do not want the software to switch between the screen, the simplest way is to find in the project Androidmanifest.xml you specify the activity in addition to the Android:screenorientation attribute, he has the following parameters:
"Unspecified": The default value is determined by the system to determine the direction of the display. The decision strategy is device-dependent, so different devices will have different display orientations. " Landscape ": Horizontal screen display (width ratio to long)" Portrait ": Vertical display (high width to long)" user ": the current preferred direction for users" behind ": the same direction as the activity below the activity ( "Sensor" in the activity stack: there is a physical sensor to decide. If the user rotates the device, the screen will switch between the screens. "Nosensor": ignores the physical sensor so that it does not change as the user rotates the device (except for the "unspecified" setting).
You can also use the Java code in the
Setrequestedorientation (Activityinfo.screen_orientation_landscape) to set.
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. You can switch layouts in the following ways:
1) in the Res directory to establish Layout-land and Layout-port directory, the corresponding layout file unchanged, such as Main.xml. Layout-land is the horizontal screen of the layout,layout-port is the vertical screen layout, the other without the tube, the simulator will automatically find.
2) use This.getresources (). GetConfiguration (). Orientation to determine whether the screen is currently horizontal or vertical and then 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 () = = Configuration _landscape) {log.i ( "info"
, "landscape" ) } else if (This.getresources () .getconfiguration () .orientation ==configuration _portrait) {log.i ( "info"
, "portrait" ) }
在onConfigurationChanged()方法中也可以检测拥有硬键盘的键盘状态
//检测实体键盘的状态:推出或者合上 if (newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){ //实体键盘处于推出状态,在此处添加额外的处理代码 } elseif(newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){ //实体键盘处于合上状态,在此处添加额外的处理代码 }
2. Reload the problem. If you do not need to reload, you can add the configuration android:configchanges= "Orientation|keyboardhidden" in Androidmanifest.xml to configure Android: Configchanges's role is what the document says: Specify one or more configuration changesthat the activity would handle itself. If not specified, the activity would berestarted if any of the these configuration changes happen in the system. This way the activity in the program does not repeat the call to OnCreate () and does not even call OnPause, Onresume. Only one onconfigurationchanged (Configuration newconfig) is called. If re-loading is required, no modifications are required. However, if you need to save the previous action content or data during the reload process, you need to save the previous data. It is then removed from the activity's OnCreate (). Of course, android:configchanges () cannot be set, otherwise the OnCreate () method will not be called.
If you want to completely prohibit flipping, you can set the Android:screenorientation property to Nosensor, so you can ignore the trouble of gravity induction. However, it does not work on the simulator and is correct on the real machine. 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.
Here is a little knowledge, the Android Simulator, the shortcut key "Ctrl+f11/f12" can realize the rotary screen
Android Horizontal screen vertical screen switching problem one, no screen conversion
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)
{
} 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 two, the activity each screen switch will recall 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.
Display display = Getwindowmanager (). Getdefaultdisplay ();
int width = display.getwidth ();
int height = display.getheight ();
if (width > height) {
orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; //横屏
} else {
orientation = activityinfo.screen_orientation_portrait; Vertical screen
}
Step Three: Append this.setrequestedorientation (orientation) to the onconfigurationchanged () function.
public void onconfigurationchanged (Configuration newconfig) {
super.onConfigurationChanged(newConfig); this.setRequestedOrientation(orientation);
}
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.
protected void Onresume () {
orientation = ActivityInfo.SCREEN_ORIENTATION_USER;
This.setrequestedorientation (orientation);
Display display = getWindowManager().getDefaultDisplay(); int width = display.getWidth(); int height = display.getHeight(); if (width > height) { orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; } else { orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; }
Super.onresume ();
}
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
Implementation of vertical screen switching function in Android system under horizontal screen
A: Different layout
Android phone screen size is different, there are 480x320, 640x360, 800x480. How can I get the app to adapt to various screens automatically?
In fact, it is very simple to create a different layout folder in the Res directory, such as layout-640x360,layout-800x480, All layout files are written to R.java after compilation, and the system uses the appropriate layout according to the size of the screen.
II: HDPI, MDPI, ldpi
In the previous version, there was only one drawable, while the 2.1 version had drawable-mdpi, drawable-ldpi, drawable-hdpi Three, and the three were mostly to support multi-resolution.
The difference between drawable-hdpi, drawable-mdpi and drawable-ldpi:
(1) drawable-hdpi contains high-resolution images, such as WVGA (480x800), FWVGA (480x854)
(2) drawable-mdpi contains medium-resolution images, such as HVGA (320x480)
(3) drawable-ldpi contains low-resolution images, such as QVGA (240x320)
The system will be based on the resolution of the machine to each of these folders to find the corresponding picture.
Three: Horizontal Screen vertical screen
In developing the program in order to be compatible with different screens on different platforms, it is recommended that the respective folders store different versions of the images according to requirements. The following steps are circulated online, but I myself was through a graphical interface to achieve this configuration, is the same as the same, I will be free to paste the picture.
Also note that each activity has this attribute screenorientation, each activity needs to be set, can be set to vertical screen (portrait), can also be set to no gravity sense (nosensor).
To keep the program interface in one Direction, do not change depending on the direction of the phone:
It can be configured in Androidmanifest.xml. Join this line
android:screenorientation= "Landscape"
For example (landscape is landscape, Portrait is portrait):
Java code:
Android Screen Toggle Summary