Summary of Android screen switching

Source: Internet
Author: User

If you don't want the software to switch between the screens:

You can do this 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!

"Unspecified": The default value is determined by the system to determine the direction of the display. The decision strategy is related to the device, so different devices will have different display directions.
"Landscape": Horizontal screen display (width ratio to long)
"Portrait": Vertical display (high width to long)
"User": the current preferred direction for users
"Behind": in the same direction as the activity below the activity (in the activity stack)
"Sensor": 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).

It can also be set in Java code via setrequestedorientation (Activityinfo.screen_orientation_landscape).

If you want your software to switch between the screen and the

Depending on the aspect of the screen, it is possible to require different layouts. 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) by 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.
1 if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
2     Log.i("info","landscape"); // 横屏
3 }
4 elseif(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
5     Log.i("info","portrait"); // 竖屏
6 }
keyboard states that have a hard keyboard can also be detected in the Onconfigurationchanged () method
1 //检测实体键盘的状态:推出或者合上   
2 if(newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_NO){
3    //实体键盘处于推出状态,在此处添加额外的处理代码
4 }
5 elseif(newConfig.hardKeyboardHidden ==Configuration.HARDKEYBOARDHIDDEN_YES){
6    //实体键盘处于合上状态,在此处添加额外的处理代码
7

< Span style= "FONT-FAMILY:SIMHEI;FONT-SIZE:18PX;" > If you do not need to load from a new , you can add 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's a little knowledge of the Android simulator, shortcut keys " ctrl+f11/f12 " can realize the rotary screen

Second, 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

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
@Overridepublic 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}}



Summary of Android screen switching

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.