Android horizontal and vertical screen switching and corresponding layout Loading Problems, android layout Loading

Source: Internet
Author: User

Android horizontal and vertical screen switching and corresponding layout Loading Problems, android layout Loading

First, the problem of horizontal and vertical screen layout when switching:

If you want the software to switch between portrait and portrait screens, the height and width of the portrait screen may be converted, and different la s may be required.

You can switch the layout in either of the following ways:

1) Create the layout-land and layout-port directories under the res directory, and the corresponding layout file names remain unchanged. For example, layout-land is the layout of a horizontal screen, layout-port is the layout of the vertical screen. When switching between the horizontal and vertical screens, the program calls setOnContent (xxx) in the onCreate method of the Activity and automatically loads the corresponding layout.

2) If the layout resource does not follow the preceding settings, you can use java code to determine whether the current layout is a horizontal screen or a vertical screen and then load the corresponding xml layout file. Because when the screen changes to a horizontal screen, the system will re-load the onCreate method of the current Activity (that is, the life cycle of the Activity needs to start again ), you can put the following method in your onCreate to check the current direction, and then let your setContentView load different layout.

/** 1: Portrait screen 2: Landscape screen determines the orientation of the screen to rotate */private int orientation; orientation = getResources (). getConfiguration (). orientation;

Second, force the horizontal and vertical screen orientation of the screen:

Android horizontal and vertical screen switching is common in mobile phone development. In order to avoid unnecessary troubles caused by horizontal and vertical screen switching during the development process of many software, you must set the direction of the horizontal and vertical screens,

You can set the android: screenOrientation attribute value in the activity in AndroidManifest. xml.

For example, the following settings:

Horizontal Screen Display Settings: android: screenOrientation = "lanscape" portrait Screen Display Settings: android: screenOrientation = "portrait"

Of course, the above modifications can also be implemented through the code in the Java code: (switching the android screen will restart the Activity, so the current Activity status will be saved before the Activity is destroyed, and load the configuration when the Activity creates again)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Third, the configuration file onConfigurationChanged is required to intercept horizontal and vertical screen switching.

The Activity will call onPause-> onStop-> onDestory-> onCreate-> onStart-> onResume (to save and read the content and data, otherwise, the content before the screen conversion will disappear)

In many cases, this result makes the program cumbersome. Therefore, Android provides setting the android: configChanges attribute in manifest, so that the Activity does not continue the above reconstruction process;

Method 1) Configure Activity in Mainfest. xml of the Android project:Android: configChanges = "keyboardHidden | orientationThe OnCreat function will not be executed after the screen switching, but the onConfigurationChanged () function will be called to control the screen switching.

Method 2) users can:OnConfigurationChanged (Configurationnew Config)Function to obtain the current horizontal and vertical screen parameters. The call sequence is similar to the Transfer sequence of touch time, but he does not have the concept of consuming events, and calls each onConfigurationChanged function in sequence.

The onConfigurationChanged method of the Activity needs to be rewritten. The implementation method is as follows. You do not need to do too much content.:

You must note that the onConfigurationChanged function can only obtain parameters after switching between the horizontal and vertical screens. In this function, the new Layout and Widget size and position information cannot be obtained, to process the size and location information, the message must be called asynchronously or in a delayed manner;

        @Override        public void onConfigurationChanged(Configuration newConfig) {                super.onConfigurationChanged(newConfig);                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {                        // land do nothing is ok                } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {                        // port do nothing is ok                }        } 

Fourth, adaptive screen switching:

If you want it to be a horizontal screen when it is started, the horizontal screen will be displayed, and the vertical screen will be displayed, and the mobile phone will not be able to switch between the horizontal and vertical screens. How can this problem be solved?

First, append in Mainfest. xml.

android:screenOrientation="sensor" android:configChanges="orientation|keyboardHidden"

Then, get the screen length and width, and compare and set the variables of the screen.

Display display = getWindowManager (). getdefadisplay display (); int width = display. getWidth (); int height = display. getHeight (); if (width> height) {orientation = ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE; // landscape screen} else {orientation = ActivityInfo. SCREEN_ORIENTATION_PORTRAIT; // portrait screen}

Then, append this. setRequestedOrientation (orientation) to the onConfigurationChanged () function)

public void onConfigurationChanged(Configuration newConfig) {       super.onConfigurationChanged(newConfig);       this.setRequestedOrientation(orientation);   }  

However, when you switch to another image and return to the original image, it is still horizontal or vertical. How can I bring it back from another screen and re-layout it?

You only need to set it in OnResume (), but this only supports layout for the horizontal and vertical screens;

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();}


Note the following three points:

1. When the android: configChanges of the Activity is not set, the life cycle of the screen is re-called, the screen is executed once, and the screen is split twice.
2. When setting the Activity's android: configChanges = "orientation", the screen will be switched to call each lifecycle, and the screen will be executed only once
3. When the android: configChanges = "orientation | keyboardHidden" of the Activity is set, the live cycle is not re-called and only the onConfigurationChanged method is executed.




When the layout of the horizontal and vertical screens of android is different, you need to reload onCreate (). How can you save the data without reloading and switch the layout?

Data is stored in ondestory or onsaveInstence. In oncreate, you can determine whether the current screen is landscape or landscape. you can load different layout files through setContentView, set the data.

Sometimes the layout file is not automatically loaded when the screen is switched between landscape and landscape. Why?

I probably know the reason, but I still don't know the exact reason. In my Mainfester. some activities in xml are android: configChanges = "keyboardHidden | orientation" and some are android: configChanges = "keyboardHidden | orientation | screenSize ", the latter will show the switching screen and will not reload the corresponding resource files. The activity is not restarted (it is said that, starting from Android 3.2 (API level 13), "screen size" also changes with the horizontal and vertical screen switching of the device. Therefore, if you are developing at the API level 13 or higher (minSdkVersion and targetSdkVersion attribute Declaration), you must prevent the restart from changing the direction, you must add the "screen size" value for "orientation .), In this case, do I have to process onConfigurationChanged in every activity to ensure that the screen loading is correct and the data is complete? This is troublesome. Do you need to add the correct method ????

Related Article

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.