Overview of Android horizontal and vertical screen Switching

Source: Internet
Author: User
Tags custom name

You only need to click the "screen rotation" button to close the screen switching. 1. Disable intra-APP horizontal and vertical screen switching. The above settings change the entire mobile phone's horizontal and vertical screen switching. If the mobile phone does not disable the horizontal and vertical screen switching function, once the system triggers the horizontal and vertical screen switching, by default, the page of the currently active App will be switched between portrait and landscape. Because the interface size and other parameters of the portrait screen are different, in the design and development of many software programs, in order to avoid unnecessary troubles during screen switching, the App usually needs to disable screen switching. set the android: screenOrientation attribute value in the activity in xml. This android: screenOrientation attribute has the following parameters: "unspecified": The default value is determined by the system. the determined policy is related to the device, so different devices have different display directions. "landscape": landscape Display (longer than width) "portrait": landscape Display (longer than width) "user": your preferred direction "behind ": consistent with the direction of the Activity under the Activity (in the Activity stack) "sensor": there is a physical sensor to decide. If you rotate the device, the screen will be switched horizontally and vertically. "Nosensor": Ignore the physical sensor so that it will not be changed as the user rotates the device (except for the "unspecified" setting ). For example, if android is set as follows: screenOrientation = "portrait", no matter how the mobile phone changes, the activity with this attribute will be displayed on a portrait screen. Android: screenOrientation = "landscape", which is displayed on a horizontal screen. You can also set setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE) 2. Can the horizontal and vertical screen switching of the APP be manually triggered? as described above, when android: screenOrientation is the default value "unspecified" or "sensor, the system will trigger switching between the horizontal and vertical Screens Based on the rotation of the device. Is there a way for us to manually trigger switching between the horizontal and vertical screens in the program, apparently, the setRequestedOrientation provided for us above is an entry provided by the system. The following is a case for triggering by pressing a key: public class MainActivity extends Activity implements OnClickListener {private Button mBtnLandscape; private Button mBtnPortrait; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); mBtnLandscape = (Button) findViewById (R. id. but_landscape); mBtnPortrait = (Button) findViewById (R. id. but_portrait); mBtnLandscape. setOnClickListener (this); mBtnPortrait. setOnClickListener (this) ;}@ Override public void onClick (View v ){// TODO Auto-generated method stub if (v = mBtnLandscape) {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_LANDSCAPE);} else {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT); }}@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); String message = newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE? "Screen setting: Landscape screen": "screen setting: Landscape screen"; Toast. makeText (this, message, Toast. LENGTH_LONG ). show () ;}} note that during manual calls, the screenOrientation settings in AndroidManifest are ignored. In addition, onConfigurationChanged in the above Code is also required to be called. Here, I will only give the code and will not discuss it. I will give a supplementary explanation later. 3. Restart the horizontal and vertical screen switching of the Activity in the above case column. By default, the screen switching of the Activity is performed (including using setRequestedOrientation) call onPause-> onStop-> onDestory-> onCreate-> onStart-> onResume to destroy the original Activity object and create a new Activity object, this is because the software usually switches between the horizontal and vertical screens, and the height and width of the interface are converted, which may require different la S. The specific layout switching can be achieved through the following two methods: 1) Create the layout-land and layout-port directories under the res directory, and the corresponding layout file names remain unchanged, such as main. xml. Layout-land is the layout of the horizontal screen, and layout-port is the layout of the vertical screen. When switching between the horizontal and vertical screens, the program calls the onCreate method of the Activity, in this way, the response layout is automatically loaded based on the current landscape. 2) If the layout resources are different and do not follow the settings above, you need to use java code to determine whether the current screen is a landscape screen or a landscape screen and then load the corresponding xml layout file (for example, mainP is a landscape screen and mainL is a landscape screen ). When the screen changes to a horizontal screen, the system will call the onCreate method of the current Activity again. You can put the following method in your onCreate to check the current direction, then, let your setContentView load different layout xml files. @ Override protected void onCreate (Bundle icicle) {super. onCreate (icicle); int mCurrentOrientation = getResources (). getConfiguration (). orientation; if (mCurrentOrientation = Configuration. ORIENTATION_PORTRAIT) {// If current screen is portrait Log. I ("info", "portrait"); // setContentView (R. layout. mainP);} else if (mCurrentOrientation = Configuration. ORIENTATION_LANDSCAPE) {// If current Screen is landscape Log. I ("info", "landscape"); // setContentView (R. layout. mainL);} init (); // initialization, assignment, and other operations findViews (); // obtain the control setListensers (); // set various listening methods of the control} The above only describes the layout switching. In fact, restarting the Activity will inevitably lead to data loss and re-acquisition without processing, in this way, the user experience will be very poor. Therefore, we need to save the data before switching, and resume the data after switching and restarting. The specific steps are as follows: rewrite Activity. onRetainNonConfigurationInstance (). Save the data @ Override public Object onRetainNonConfigurationInstance () {final MyDataObject data = collectMyLoadedData (); return data;} In onCreate () the function calls getLastNonConfigurationInstance () to obtain the data saved by onRetainNonConfigurationInstance () @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInsta NceState); setContentView (R. layout. main); final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance (); if (data = null) {data = loadMyData ();}...} 4. Non-Restart Activity horizontal and vertical screen switching although restarting Activity provides us with the method of saving data and reading data, the program will become a little cumbersome, so sometimes programmers often don't want to restart the Activity, and Android also provides us with a solution, that is, to intercept horizontal and vertical screen transformations through onConfigurationChanged, so as to re-layout and switch the necessary operations. The procedure is as follows: first, set the android: configChanges attribute for the corresponding Activity in manifest, so that the Activity does not continue the above reconstruction process, as follows: android: configChanges = "orientation | keyboardHidden" can be used in sdks earlier than Andorid 3.2. The SDK after Adnroid 3.2 must add a screenSize attribute, as shown in the following figure: configChanges = "keyboardHidden | orientation | screenSize" or android: configChanges = "orientation | screenSize". A detailed description of configChanges is provided later. Do not expand it too much here. Second, obtain the current horizontal and vertical screen parameters in the Activity or View onConfigurationChanged (Configuration newConfig) function. As for its call sequence, it is similar to the Transfer sequence of touch events, but he does not have the concept of consuming events, and will call each onConfigurationChanged function in sequence. The following is an example of overwriting an Activity: // the layout is in the layout-land and layout-port directories with the same name main. @ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); setContentView (R. layout. main); // note that init () is deleted here. Otherwise, the findViews (); setListensers ();} is lost after initialization ();} // The layout is not based on the layout-land and layout-port directories, while the custom name @ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigur AtionChanged (newConfig); int mCurrentOrientation = getResources (). getConfiguration (). orientation; if (mCurrentOrientation = Configuration. ORIENTATION_PORTRAIT) {// If current screen is portrait setContentView (R. layout. mainP); // note that init () is deleted here. Otherwise, the findViews (); setListensers ();} else if (mCurrentOrientation = Configuration is lost after initialization. ORIENTATION_LANDSCAPE) {// If current screen is landscape s EtContentView (R. layout. mainL); // note that init () is deleted here; otherwise, the findViews (); setListensers ();} is lost after initialization ();}} of course, you can directly call the original control without changing the layout, for example, public class MainActivity extends Activity {private TextView textView; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); Log. I ("-- Main --", "onCreate"); textView = (TextView) findViewById (R. id. TV _id) ;}@ Override public void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); Log. I ("-- Main --", "onConfigurationChanged"); if (newConfig. orientation = Configuration. ORIENTATION_LANDSCAPE) {textView. setText ("current screen is landscape");} else {textView. setText ("current screen is landscape") ;}} note that only the parameters after landscape switching can be obtained in the onConfigurationChanged function, the size and position information of the new Layout and control cannot be obtained in this function. to process the size and position information, you must use the asynchronous or The following code re-sets the position of popupWindow when switching between the horizontal and vertical screens: @ Override protected void onConfigurationChanged (Configuration newConfig) {super. onConfigurationChanged (newConfig); // you do not need to create Handler in the View. You can directly call the post operation // new Handler (). postDelayed (new Runnable () {// @ Override // public void run () {// updatePopup (); //}, 500 ); postDelayed (new Runnable () {@ Override public void run () {updatePopup (); //}, 500); // if not In post, but directly called, the pop-up location will be problematic} Although the explicit call to the layout is not seen above to re-layout, the control object is not destroyed, however, you must re-layout and measure the control during screen switching, and re-paint the control. Otherwise, the position of the pop-up box will not change, as for how to call the relayout, measure, and Draw operations, we will not expand them here. 5. For AndroidManifest. after adding the xml settings, we can see that the specific implementation involves the screenOrientation and configChanges parameters of the specific Activity in the Manifest project configuration. The screenOrientation priority of these two parameters is higher than configChanges, that is, if the screenOrientation is set to a fixed screen, the configChanges parameter cannot be set to a landscape switch, unless you manually call the setRequestedOrientation function in the code to modify the screen.

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.