The screen orientation is for activity, so you can use the <activity> tag in the Androidmanifest.xml file. The Screenorientation property is set, for example:
XML code
- <activity
- android:name=". Sketchpadactivity "
- android:screenorientation="Landscape"/><!--so that activity is always displayed as a horizontal screen--
There are 7 optional values in the Screenorientations property (constants are 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), the display of higher than the width of the 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, and when the device is rotated, the direction changes-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:
Java code
- /*
- * Dynamically changing 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 it is not clear landscape or portrait, then confirm the actual display direction through the method of wide and high ratio
- //This will guarantee that the Orient final value will be clear horizontal screen landscape or vertical screen portrait
- if (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);
- }
- }
The Setrequestedorientation (XXX) method setting is 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 specifying the direction explicitly by the above routines. To recover, call Setrequestedorientation (UNSPECIFIED) again.
In addition, you can use the configuration object to get the current display direction of the activity:
Java code
- Confirm the current display direction with the configuration object
- 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";
- Default
- 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 the square (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 change in the screen orientation) , and it implements the reload of the resource by terminating and restarting the activity . If we declare to them that we are going to handle (some) configuration changes ourselves, we must be responsible for reloading the associated resources-that is, the target activity will not go through the process of terminating and restarting when (some) configuration changes .
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:
Java code
- <activity
- Android:name=". MyActivity "
- android:configchanges="Orientation"
- Android:label="@string/app_name"/>
In fact, the configchanges attributes are: 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 methods, such as:
Java code
- Callback when device configuration is changed
- Public void Onconfigurationchanged (Configuration conf) {
- //super.onconfigurationchanged (conf);
- //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.
Detailed screen orientation in Android