Screen Orientation is for activity, so you can use the <activity> tag in the androidmanifest.xml file screenorientation property to set, for example:
<activity android:name= ". Sketchpadactivity " android:screenorientation=" Landscape "/><!--let the activity always appear as a horizontal screen--
The Screenorientations property has 7 selectable values (constants defined in the android.content.pm.ActivityInfo class ) :
- Landscape: Horizontal screen (landscape photo), the width of the display is greater than the height;
- Portrait: Vertical Screen (Portrait photo), when displayed High degree greater than Wide degree ;
- User: The current preferred direction for users;
- Behind: Inherits the active direction of the activity below the activity stack;
- sensor: The physical sensor determines the direction of the display, depending on how the user holds the device, when Equipment the direction will change when rotated-between the horizontal screen and the vertical screen;
- Nosensor: Ignoring physical sensors-that is, the display direction is independent of the physical sensor, no matter how the user rotates the display direction of the device will not change (except for the "unspecified" setting);
- unspecified: Unspecified, this is the default value, by the Android system to choose the appropriate direction, the selection strategy depends on the configuration of the specific device, so different devices will have a different direction choice;
The above configuration values are reflected in the return value of the Activity.getrequestedorientation () method, and the corresponding Setrequestedorientation () method can dynamically change the value of the property in the form of an API. The following example toggles between two directions in a horizontal/vertical screen:
/* * dynamically changes the display direction of the current screen via API */public void Apichangeorientation () {//Get current screen orientation int orient = Getrequestedorientation (); Logger.get (). I ("Orientation:" + myutils.getorientationname (Orient));//If not explicitly landscape or portrait The actual display direction is confirmed by the method of wide and high ratio //This will guarantee that the Orient final value will be clear horizontal screen landscape or vertical screen portraitif ( Orient! = activityinfo.screen_orientation_landscape&& Orient! = activityinfo.screen_orientation_portrait) { //width > Height for horizontal screen, anyway for vertical screen int[] size = Myutils.getdisplaysize (this); orient = Size[0] < size[1]? activityinfo.screen_orientation_portrait:activityinfo.screen_orientation_landscape; logger.get (). I ("w/h:" + Myutils.getorientationname (Orient));} if (orient = = activityinfo.screen_orientation_portrait) {setrequestedorientation (activityinfo.screen_orientation_ LANDSCAPE);} Else{setrequestedorientation (activityinfo.screen_orientation_portrait);}}
by setrequestedorientation (XXX) The method settings are equivalent to the configuration in the Androidmanifest.xml file, so the activity will no longer automatically switch between the vertical and horizontal screens based on the physical sensor after the above routines have been explicitly specified, and then call Setrequestedorientation ( UNSPECIFIED).
in addition, You can also use the configuration object to get the current display direction of the activity:
The configuration object confirms the current display direction of the configuration conf = Getresources (). GetConfiguration (); String orientname = "undefined"; switch (Orient) {case Configuration.ORIENTATION_LANDSCAPE:orientName = "LANDSCAPE"; Case Configuration.ORIENTATION_PORTRAIT:orientName = "PORTRAIT", Case Configuration.ORIENTATION_SQUARE:orientName = " Square ";d efault:orientname =" undefined ";} Logger.get (). I ("conf.orient:" + orientname);
It is important to note that the two (Activityinfo and configuration) constants defined for the direction are inconsistent, and the constants in the Activityinfo are the policies used for the direction of the decision display, and the constants in the configuration objects are explicit The actual display direction, a total of 4 possible: undefined (UNDEFINED), horizontal screen (LANDSCAPE), vertical screen (PORTRAIT) , and squares (square).
Finally, let's take a look at how to capture events that show directional changes in the program, starting with the Activity.onconfigurationchanged method.
The Android system reloads different resource files (such as layout layouts resources) based on changes in the configuration of the device (such as the screen orientation) , which is implemented by terminating and restarting the activity Resources of the Reload . If we declare to them that we are going to handle (some) configuration changes ourselves, we must be responsible for the reloading of the associated resources -that is, the target activity will not go through when (some) configuration changes The process of terminating and restarting.
We focus on the changes in the display direction and need to specify that the Configchanges property equals orientation when the activity is declared, as shown in the following example:
<activityandroid:name= ". MyActivity "android:configchanges=" orientation "android:label=" @string/app_name "/>
in fact, the Configchanges property also has: Fontscale (user first font size change), locale (user's locale change), keyboard (keyboard type change) and many other optional values .
Then overwrite the Activity.onconfigurationchanged method, for example:
Callback public void onconfigurationchanged (configuration conf) {//super.onconfigurationchanged (CONF) when device configuration changes;//todo: You Process ...}
this will cause the above method to be called back when the screen orientation changes. Again, the onconfigurationchanged is called back only if the configuration item specified in Configchanges changes, so the above method is only recalled when the screen orientation (orientation) changes.
A simple example of how Android dynamically changes screen orientation (Landscape and portrait)! android mobile phone screen in the direction of the dynamic, we may also have the mobile phone will have this function, when the direction of our mobile phone changes, the screen will also change, in this Android is very easy to achieve.
We mainly use the getrequestedorientation(), and the setrequestedorientation() two methods here. However, to use these two methods must first set the screen side of the Androidmaniefst.xml property, or the program will not work properly.
The main code is as follows:
If it is vertical, it is changed to horizontal if (getrequestedorientation () = = Activityinfo.screen_orientation_landscape) { Setrequestedorientation (activityinfo.screen_orientation_portrait); } If it is horizontal, then the else if (getrequestedorientation () = = activityinfo.screen_orientation_portrait) is instead vertical Setrequestedorientation (Activityinfo.screen_orientation_landscape); }
set the default direction in the androidmanifest.xml file, or the program will not work properly oh. The code is as follows:
<activity android:name= ". Activity01 " android:label=" @string/app_name " android:screenorientation=" Portrait ">
Detailed screen orientation in Android