Android Screen switch processing

Source: Internet
Author: User

Android screen to solve the problem should be two:

1. Layout issues; 2. Reload the problem

I. Layout issues:

If you do not want the software to switch between the two screens, 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 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 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) 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.

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
}

Keyboard states that have a hard keyboard can also be detected in the Onconfigurationchanged () method

Check the status of the physical keyboard: eject or close   
if (Newconfig.hardkeyboardhidden ==configuration.hardkeyboardhidden_no) {
The physical keyboard is in the roll-out state, where additional processing code is added
}
else if (Newconfig.hardkeyboardhidden ==configuration.hardkeyboardhidden_yes) {
The physical keyboard is in a closed state, where additional processing code is added
}

Two. Reload the question:

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" No matter how the mobile phone changes, the activity with this attribute will be the vertical screen display. 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

Three. 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
}
}

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.