This article describes the method of Android programming to determine horizontal and vertical screens and set horizontal and vertical screens. Share it for your reference, as follows:
It is still this mobile phone project. One requirement is that only the interface that has just entered is allowed to be displayed horizontally in the entire project, and the title of the interface must be hidden. When switching back to the vertical display, the title becomes visible. At the beginning, I asked Baidu for a monitoring function that monitors whether the activity is horizontal and vertical, as follows:
// If the screen does not reload the activity, call this method@Override public void onConfigurationChanged(Configuration newConfig) { (newConfig); // If it's a screen try { // Checks the orientation of the screen if ( == Configuration.ORIENTATION_LANDSCAPE) { (); } else if ( == Configuration.ORIENTATION_PORTRAIT) { (); } } catch (Exception ex) { } }
I originally thought that the function of this monitoring function was to hide the title when the phone was horizontally screened and display it in vertical screen. It seemed logically correct. When I tested the virtual machine, I felt that it was fine. After several days, a very strange bug was reported to the test, which was described as follows:
When the testers were testing, they did not enter the main interface according to the regular vertical screen, but mischievously entered from the login interface horizontal screen. Then the problem arose. The floating menu on the left originally occupied half of the screen, but now it suddenly became full of the entire screen, and the title is still there. This is irrelevant (the width of the floating menu is calculated based on 1/2 of the width of the screen)?
The problem that arises isonConfigurationChanged
The monitoring method is performed after the screen switches. When the tester has entered horizontally, he does not enter this function for hidden operations at all. Moreover, when the horizontally screen enters horizontally, the width is the height of the phone, which makes the menu display abnormally.
Since you know the problem, it's easy to deal with. Here are the solutions:
After the user logs in from the login interface, set the change interface to a vertical screen display (this step is to let the floating menu calculate the pop-up width). After the execution, reset the change interface to support horizontal and vertical screen display. The code is as follows:
/** * Added when the quote interface is loaded for the first time, the user screen enters the hidden title, qiulinhe, November 5, 2015 17:33:06 */setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//The first time you enter it is a vertical screen displaytoolbarLayout = (RelativeLayout) (.main_toolbar); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);//Support horizontal and vertical screensDisplay display = getWindowManager().getDefaultDisplay();//Calculate the width and height of the phone at this time to determine whether the title needs to be displayed.int width = (); int height = (); if (width > height) { (); } else { ();// Vertical screen} toolbarLayout = (RelativeLayout) (.main_toolbar); ();
Then use the attributes provided by the system to get the width and height of the phone at this time, determine whether it is a vertical or horizontal screen, and then set the title. After writing, the task is done.
There is a lot of knowledge about the configuration of activity in the main configuration file on the Internet, so I won’t talk about it.
For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android's SQLite database skills》、《Summary of Android operating json format data skills》、《Android resource operation skills summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.