Summary of examples of Android horizontal and vertical screen switching

  • 2020-12-20 03:44:18
  • OfStack

This article summarizes the Android horizontal and vertical screen switching skills. To share for your reference, the details are as follows:

1. Do not switch between horizontal and vertical screens

Android horizontal and vertical screen switching is quite common in mobile phone development. In order to avoid unnecessary troubles caused by the horizontal and vertical screen switching in the development process, many software usually forbid the horizontal and vertical screen switching, that is, by setting the attribute value of android:screenOrientation in ES7en.xml.

The android:screenOrientation attribute, which has the following parameters:

"unspecified": The default value is determined by the system. The determined strategy is device dependent, so different devices will have different display directions.

"landscape": Landscape display (width to height)

"portrait": Portrait display (height longer than width)

"user": The current preferred direction of the user

"behind": pointing 1 below the Activity (in the Activity stack)

"sensor": There are physical sensors to determine. If the user rotated the device, the screen would switch between vertical and horizontal.

"nosensor": Ignore the physical sensors so that they do not change as the user rotates the device (except for the "unspecified" setting).

Such as the following Settings


activity with this property will be portrait no matter how the phone changes.

For landscape display.

The above changes can also be set in the Java code with a code similar to the following


2. Switch between vertical and horizontal screens to change the layout

If you want to switch between the horizontal and vertical screens, you may need a different layout because the height and width of the screens will change. You can switch layouts in two ways:

1) Create ES62en-ES63en and ES64en-ES65en directories under res, and the corresponding layout filename remains unchanged, such as ES67en.xml. layout-land is layout for landscape, ES72en-ES73en for portrait, and the rest is ignored. When switching between portrait and portrait, the program calls the onCreate method of Activity to load the layout of the response.

2) If the layout resource is not set as above, the java code can be used to determine whether the current layout is landscape or portrait, and then the corresponding xml layout file can be loaded. Because when the screen goes to landscape, the system calls the current Activity's onCreate method again, you can put the following method in your onCreate to check the current direction, and then ask your setContentView to load the different layout xml.

if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
   Log.i("info", "landscape"); //  landscape 
else if (this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT) {
  Log.i("info", "portrait"); //  Vertical screen 

3. Intercept horizontal and vertical transformations through onConfigurationChanged

As per 2, Activity re-invokes onPause- with each vertical and horizontal switch > onStop- > onDestory- > onCreate- > onStart- > onResume, which involves saving and reading content and data that would otherwise disappear before the screen turns. In many cases, this results in cumbersome procedures, so Android provides android:configChanges attribute set in manifest, so that Activity does not continue the above reconstruction process. If the user cares about the vertical and horizontal switching, the current vertical and horizontal parameters can be obtained from the Activity or View's onConfigurationChanged(Configuration newConfig) function. As for the order in which it is called, it is similar to the order in which touch time is passed, except that it has no concept of consuming events and calls each onConfigurationChanged function in turn.

It is important to note that onConfigurationChanged function only to obtain the parameters of the screen after switching, in this function get less than new Layout and controls the size of the location information, if the size and location information to be processed, must be made by means of asynchronous messaging or delay call, below is I need somehow screen when switching in the project needs to be set up popupWindow location code:

protected void onConfigurationChanged(Configuration newConfig) {
   // TODO Auto-generated method stub
   //View You don't have to create Handler Can be called directly post operation 
//      new Handler().postDelayed(new Runnable() {
//          @Override
//          public void run() {
//             updatePopup();
//          }
//      }, 500);
   postDelayed(new Runnable() {
       public void run() {
          updatePopup();   //
   }, 500);// Try using it directly post Operation, the updatePopup You can also get the correct position in the function 

As for horizontal and vertical screen flip, how the system to make the window flip and redraw, I also did not go to the source code, so it is not clear, there is understanding can tell my younger brother.

Of course, if you want to ban flipping completely, you can set the android:screenOrientation property to nosensor, so that you can ignore the gravity sensing trouble. But it doesn't work on the simulator, it works on the real machine.

Here is a little knowledge, in the Android simulator, the shortcut key "Ctrl+F11/F12" can realize the screen transfer

I hope this article has been helpful in Android programming.

Related articles: