Android manually switches the screen direction

Source: Internet
Author: User

Screen direction parameters:
 

Activityinfo. screen_orientation_unspecified, // Unspecified. This is the default value. The Android system selects the appropriate direction.
Activityinfo. screen_orientation_landscape, // landscape
Activityinfo. screen_orientation_portrait, // portrait Screen
Activityinfo. screen_orientation_user ,// User's current preferred direction
Activityinfo. screen_orientation_behind, // inherits the direction of the activity under the current activity in the activity Stack
Activityinfo. screen_orientation_sensor, // The display direction is determined by the physical sensor.
Activityinfo. screen_orientation_nosensor ,// Ignore the physical sensor-that is, the display direction is irrelevant to the physical sensor.
Activityinfo. screen_orientation_sensor_landscape,
Activityinfo. screen_orientation_sensor_portrait,
Activityinfo. screen_orientation_reverse_landscape,
Activityinfo. screen_orientation_reverse_portrait,
Activityinfo. screen_orientation_full_sensor,

You can use setrequestedorientation (activityinfo. screen_orientation_unspecified) to specify the screen direction.

ExampleCode:

31. dynamically change the screen direction
/If it is a vertical bar, it is changed to a horizontal bar.
If (getrequestedorientation () = activityinfo. screen_orientation_landscape)
{
Setrequestedorientation (activityinfo. screen_orientation_portrait );
}

// If the row is horizontal, it is changed to vertical

Elseif (getrequestedorientation () = activityinfo. screen_orientation_portrait)
{
Setrequestedorientation (activityinfo. screen_orientation_landscape );
}

Set the default direction in the androidmanifest. xml file.
<Activity Android: Name = ". changeorientation"
Android: Label = "@ string/app_name"
Android: screenorientation = "portrait">

32. Prevent the oncreate operation from being re-executed after the screen Rotation
  You only need to add attributes to the activity configuration file:
      Android: configchanges = "orientation | keyboardhidden | navigation"
      
      You can overload the onconfigurationchanged method in the activity and perform other actions according to different rotation directions, as shown below:

      @ Override
      Public void onconfigurationchanged (configurationnewconfig ){
         Super. onconfigurationchanged (newconfig );
         
      }

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.