Detailed explanation of Android horizontal and vertical screen switching and its corresponding layout loading

  • 2021-09-05 00:46:34
  • OfStack

This article shares the Android horizontal and vertical screen switching and its corresponding layout loading problems for your reference. The specific contents are as follows

First, the horizontal and vertical screen switching is associated with the horizontal and vertical screen layout:

If you want the software to switch between horizontal and vertical screens, different layouts may be required because the height and width of horizontal and vertical screens will change.

You can switch layouts in two ways:

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

2) If the layout resources are not set as above, the java code can be used to judge whether the current horizontal screen or vertical screen and then load the corresponding xml layout file. Since the onCreate method of the current Activity will be reloaded when the screen becomes horizontal (that is, the life cycle of the Activity will start over), you can put the following methods in your onCreate to check the current direction, and then have your setContentView load different layout.


/** 1: Vertical screen   2: Horizontal screen   Determine the direction in which the screen rotates  */
  private int orientation;
orientation=getResources().getConfiguration().orientation;

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

Switching between horizontal and vertical screens of Android is common in mobile phone development. In order to avoid unnecessary troubles caused by switching between horizontal and vertical screens in the development process of many software, it is usually necessary to set the direction of horizontal and vertical screens by setting the attribute values of android: screenOrientation in AndroidManifest. xml.

For example, the following settings:

Horizontal display settings: android: screenOrientation= "lanscape"

Vertical display settings: android: screenOrientation = "portrait"

Of course, the above modification can also be realized by code in Java code: (android screen switching will restart Activity, so save the current active state before Activity is destroyed, and load the configuration when Activity restarts Create)


setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

Third, intercept the configuration file required for horizontal and vertical screen switching: onConfigurationChanged

Activity will call onPause again every time the horizontal and vertical screens are switched- > onStop- > onDestory- > onCreate- > onStart- > onResume (Save and read the content and data for this purpose, otherwise the content before the screen change will disappear)

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

Mode 1) Configure Activity: android: configChanges= "keyboardHiddenorientation in Mainfest. xml of Android project. After switching between horizontal and vertical screens, OnCreat function will not be executed, but onConfigurationChanged () will be called, so that the switching between horizontal and vertical screens can be controlled.

Mode 2) The user can get the current horizontal and vertical screen parameters in the Activity or View: onConfigurationChanged (Configurationnew Config) function. As for its calling order, it is similar to the transfer order of touch time, but he has no concept of consumption events, and will call every onConfigurationChanged function in sequence.

You need to override the onConfigurationChanged method of Activity. The implementation is as follows, without much to do:

It should be noted that only the parameters after the horizontal and vertical screens are switched can be obtained in the onConfigurationChanged function, and the new Layout and the size and position information of the control cannot be obtained in this function. If the size and position information are to be processed, it must be called asynchronously or delay through messages;

@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, adaptively switch the screen:

If you want it to start with a horizontal screen, it will be indicated by a horizontal screen, and if it is indicated by a vertical screen, it will be indicated by a vertical screen. Then the mobile phone can't be used when switching between horizontal and vertical screens. How to solve this problem?

First: Add android: screenOrientation= "sensor" android: configChanges= "orientationkeyboardHidden" to Mainfest. xml

Then: Get the length and width of the screen, compare and set the variables of horizontal and vertical screens.


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

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


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

But if you cut to another picture and go back to the original picture, it will still be horizontal or vertical. How to make it come back from other screens and re-arrange the horizontal and vertical screens?

As long as it is set in OnResume (), but this only supports only one layout on 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();
  }

There are three points to note:

1. When android: configChanges of Activity is not set, each life cycle will be called again when cutting the screen, which will be executed once when cutting the horizontal screen and twice when cutting the vertical screen
2. When android: configChanges= "orientation" of Activity is set, each life cycle will be called again when cutting the screen, and it will only be executed once when cutting the horizontal screen and vertical screen
3. When android of Activity is set: configChanges= "orientationkeyboardHidden", the screen cutting will not call each life cycle again, but will only execute the onConfigurationChanged method


Related articles: