SoFunction
Updated on 2025-03-11

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

This article shares the Android horizontal and vertical screen switching and its corresponding layout loading issues for your reference. The specific content is as follows

First, horizontal and vertical screen switching and horizontal and vertical screen layout problem:

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

There are two ways to switch layouts:

1) Create layout-land and layout-port directories in the res directory, and the corresponding layout file name remains unchanged. For example: layout-land is a layout for horizontal screen, and layout-port is a layout for vertical screen. Don't worry about anything else. When switching horizontal and vertical screens, the program calls setOnContent(xxx) in the onCreate method of Activity and automatically loads the corresponding layout.

2) If the layout resource is not set as above, you can use Java code to determine whether it is currently horizontal or vertical screen and then load the corresponding xml layout file. Because when the screen becomes horizontal, the system will reload the onCreate method of the current activity (that is, the life cycle of the activity must start over). You can put the following method in your onCreate to check the current direction, and then let your setContentView load different layouts.

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

second, Force the horizontal and vertical screen directions of the screen:

Android horizontal and vertical screen switching is common in mobile phone development. In order to avoid unnecessary troubles during horizontal and vertical screen switching, many software usually have to force the direction of horizontal and vertical screens to set the android:screenOrientation property value in the activity.

For example, the following settings:

Horizontal display settings:android:screenOrientation="lanscape"

Vertical screen display settings:android:screenOrientation="portrait"

Of course, the above modification can also be implemented in Java code through code: (The switching of the Android screen will restart the Activity, so the current active state is saved before the Activity is destroyed, and the configuration is loaded when the Activity creates again)

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

third, intercept horizontal and vertical screen switching required configuration file: onConfigurationChanged

Activity will call onPause->onStop->onDestory->onCreate->onStart->onResume every time you switch horizontally and vertically (you must save and read this content and data, otherwise the content before turning the screen will disappear)

Many times, such results make the program cumbersome, and for this reason, Android provides the setting of the android:configChanges attribute in manifest, so that the Activity does not continue the above reconstruction process;

Method 1) Configure Activity in Android project:android:configChanges="keyboardHidden|orientation , after switching horizontal and vertical screens, the OnCreat function will not be executed, but will call onConfigurationChanged() so that the horizontal and vertical screens can be switched.

Method 2) Users can use Activity or View:onConfigurationChanged(Configurationnew   Config), get the current horizontal and vertical screen parameters in the function. As for the order of calling is similar to the order of passing touch time, it does not have the concept of consuming events and will call each onConfigurationChanged function in sequence.

The onConfigurationChanged method of the Activity needs to be overridden. The implementation method is as follows, and there is no need to do much content:

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

@Override
        public void onConfigurationChanged(Configuration newConfig) {
                (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 switching screen:

If you want it to be horizontal when it is started, it means horizontally. If it is vertically, it means vertically. Then, the phone cannot use horizontally and vertically. How can I solve it?

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

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

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

Then: append (orientation) in the onConfigurationChanged() function

public void onConfigurationChanged(Configuration newConfig) { 
         (newConfig); 
         (orientation); 
       } 

But in this case, when you cut to another picture and return to the original picture, it will still be horizontal or vertical. How can it be re-layed horizontal and vertical screen after it comes back from another screen?

Just set it in OnResume(), but this only supports horizontal and vertical screens with only one layout;

protected void onResume() {
    orientation = ActivityInfo.SCREEN_ORIENTATION_USER;
    (orientation);
    Display display = getWindowManager().getDefaultDisplay();
    int width = ();
    int height = ();
    if (width > height) {
      orientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
    } else {
      orientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
    }
    ();
  }

There are three pointsPlease note

1. When the activity's android:configChanges are not set, the screen-cutting will call each life cycle again. It will be executed once when cutting horizontal screen, and it will be executed twice when cutting vertical screen.
2. When setting the activity's android:configChanges="orientation", the screen-cutting will still call each life cycle again, and the horizontal and vertical screens will only be executed once.
3. When setting the activity's android:configChanges="orientation|keyboardHidden", the screen-slicing will not re-call each life cycle, and will only execute the onConfigurationChanged method.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.