Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the switching between horizontal and vertical screens in Android development

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to switch between horizontal and vertical screens in Android development. many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

When the phone is placed horizontally / vertically in the App, the horizontal / vertical screen of the screen can be switched. Record the key information and implementation process needed in the middle.

To put it bluntly, there are two ways to achieve automatic / manual rotation of the screen:

One is defined in the code of the project, which is performed when switching between landscape and vertical screens: destroy the current Activity- and rebuild the Activity based on the new screen size. If the data storage operation is not carried out, the data in the Activity will be lost during the switching process.

The other is defined in the AndroidManifest.xml of the project. In some cases, "do not destroy the Activity that requires horizontal and vertical screens" can be achieved, because this method will not destroy the Activity and rebuild the Activity, so the data of the Activity will not be lost.

Next, we will introduce these two implementation methods respectively:

Method 1: define in the code

Add the following statement to the onCreate method in the Activity that needs horizontal screen, and require the statement to precede the setContentView (* *) statement in the onCreate method.

SetRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_FULL_SENSOR)

In the previous statement, the constant SCREEN_ORIENTATION_FULL_SENSOR is the parameter that determines how the screen rotates. Summarize the functions corresponding to all the parameters:

SCREEN_ORIENTATION_BEHIND inherits the direction of the Activity under the current Activity in the Activity stack SCREEN_ORIENTATION_FULL_SENSOR is determined by the gravity sensor 0max 90max 180x270 °SCREEN_ORIENTATION_FULL_USER SCREEN_ORIENTATION_LANDSCAPE always landscape SCREEN_ORIENTATION_PORTRAIT always vertical SCREEN_ORIENTATION_LOCKED lock screen direction SCREEN_ORIENTATION_NOSENSOR turn off the influence of the gravity sensor on the horizontal / vertical screen SCREEN_ORIENTATION_REVERSE_LANDSCAPE in the other direction landscape SCREEN_ORIENTATION_ Vertical screen in the other direction of the REVERSE_PORTRAIT (inverted mobile phone) SCREEN_ORIENTATION_SENSOR gravity sensor affects the direction of the screen 0max 90x270 °SCREEN_ORIENTATION_SENSOR_LANDSCAPE always horizontally The horizontal screen SCREEN_ORIENTATION_SENSOR_PORTRAIT in which direction is determined by the gravity sensor is always vertical, and the vertical screen SCREEN_ORIENTATION_UNSPECIFIED in which direction is determined by the gravity sensor does not specify the direction. The use of the default direction SCREEN_ORIENTATION_USER is determined by both the user and the gravity sensor. For details, at the end of the text, the SCREEN_ORIENTATION_USER_LANDSCAPE user and the gravity sensor work together to determine which direction the horizontal screen SCREEN_ORIENTATION_USER_PORTRAIT user and the gravity sensor jointly determine which direction the vertical screen UIOPTION_SPLIT_ACTION_BAR_WHEN_NARROW is. When the screen is narrow, part of the navigation bar will be displayed at the bottom.

Since the data of the corresponding Activity will be lost when switching between landscape and vertical screens in this mode, the following two methods can be overridden in the corresponding Activity to ensure that the data is not lost:

@ Overrideprotected void onSaveInstanceState (Bundle outState) {super.onSaveInstanceState (outState); / / is called before switching between landscape and vertical screens to save the data that the user wants to save. The following is a sample outState.putString ("name", "yoosir"); outState.putInt ("age", 24); outState.putBoolean ("handsome", true);} @ Overrideprotected void onRestoreInstanceState (Bundle savedInstanceState) {super.onRestoreInstanceState (savedInstanceState) / / the data stored by the user is called after screen switching. The following is an example: if (savedInstanceState! = null) {int age = savedInstanceState.getInt ("age"); String name = savedInstanceState.getString ("name"); boolean isHandsome = savedInstanceState.getBoolean ("handsome");}}

Parameter function

Method 2: define in AndroidManifest.xml

Configure android:configChanges and screenOrientation in the corresponding Activity attribute definition in AndroidManifest.xml. The android:configChanges configuration in the reference article is very clear. I will post the conclusion directly:

Android:configChanges= "orientation | keyboardHidden | screenSize"

When the configChanges is configured as above, screen cutting will not re-call each lifecycle, only the onConfigurationChanged method will be executed. The data in Activity is not destroyed. When configChanges is not configured or configChanges is configured other than the above configuration, screen cutting will re-invoke each lifecycle of the current Activity. The data in the Activity is destroyed.

The code of the example is as follows:

All possible parameters for android:screenOrientation in the above code are configured as follows.

Unspecified default value. The system automatically selects the screen direction behind and the direction of the next activity in the activity stack is the same as the landscape horizontal screen direction, the width ratio high length portrait vertical screen direction is displayed, and the height ratio width length sensor displayed is determined by the device's physical direction sensor. If the user rotates the device, the screen will switch horizontally and vertically and nosensor ignores the physical direction sensor. In this way, the horizontal and vertical screen will not be switched as the user rotates the device (except the "unspecified" setting). The current preferred direction for user users is above reverseLandscape API 9, reverse horizontal screen reversePortrait API 9 above, reverse vertical screen sensorLandscape API 9 above, horizontal screen, but you can switch forward and backward horizontal screen above sensorPortrait API 9 and vertical screen according to the physical direction sensor, but you can switch forward and reverse vertical screen fullSensor API 9 or above according to the physical direction sensor. Up and down, left and right four directions, determined by the physical direction sensor locked API 18 or above, lock the direction of the current screen

All possible parameters for android:configChanges in the above code are configured as follows:

The country code (MCC) of the mcc IMSI mobile station changes-a SIM is detected and updates the network code (MNC) of the MCC mnc IMSI mobile station changes-a SIM is detected and the update MNC locale area changes-the user selects a new language keyboard keyboard type that the text needs to be displayed changes-for example, the user inserts an external keyboard. The accessibility of the keyboardHidden keyboard has changed-for example, the user has discovered the hardware keyboard. The screenLayout screen layout changes-this causes a different Activity to be displayed. The orientation of the orientation screen changes-the user rotates the screen. Note: if the application's target API level is 13 or higher (declared by attribute minSdkVersion and attribute targetSdkVersion), you also need to declare the configuration item screenSize, as this will change when the device selects the portrait and screen orientation. The currently available screen size of screenSize has changed. This represents a change in the currently available size, which is related to the current ratio, so it changes when the user selects different images and images. However, if your program's target API level is 12 or lower, your Activity will always handle this configuration change on its own (this change will not cause an Activity restart, even on Android 3.2 or newer devices). Added in API level 13. Changes in the physical screen size of smallestScreenSize. Regardless of the change of direction, only when the actual physical screen packaging changes, such as an external monitor. The change in this configuration item causes the change in smallestWidth configuration. However, if your program's target API level is 12 or lower, your Activity will handle the change itself (this change will not cause an Activity restart, even on Android 3.2 or newer devices) added in API level 13. The direction of the layoutDirection layout changes. For example, the mode of writing changes from left to right (LTR) to right to left (RTL).

After the above attributes are configured, the data of the Activity switching between landscape and portrait screens will not be lost. If you want to display different UI or do different things according to different screen directions, you need to override the following methods in the Activity:

@ Overridepublic void onConfigurationChanged (Configuration newConfig) {super.onConfigurationChanged (newConfig); / / add the operation after screen switching here}

Value description

Outside the country

According to the above method, I realized the screen rotation according to the gravity sensor. Although the screen can be switched with the gravity sensor, I found that no matter whether the "screen rotation" or "orientation lock" in the phone settings is on or off, the Activity in the App will switch the horizontal and vertical screen according to the horizontal and vertical direction of the phone. What I want to achieve is that when screen rotation is turned on, the Activity in App follows the gravity sensor; when screen rotation is turned off, the Activity in App is fixed to the default orientation. How can App turn off the screen automatically when the gravity sensor is turned off?

Android:screenOrientation= "sensor"

Change the above code to the following code

Android:screenOrientation= "user"

This means that when the parameter is sensor, the specific Activity in the App will change the horizontal and vertical screen according to the gravity sensor, regardless of whether the screen rotation setting is turned off or not. When the parameter is user, when "screen rotation" is turned on, the specific Activity changes the horizontal and vertical screen according to the gravity sensor; when "screen rotation" is turned off, the specific Activity will fix the default direction (usually the front vertical screen).

After reading the above, do you have any further understanding of how to switch between horizontal and vertical screens in Android development? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report