Android Program Development: (8) handling changes in the screen direction -- 8.4 controlling the display direction of the activity

Source: Internet
Author: User
Occasionally, you may want to make sure that your application is displayed in either landscape or Landscape mode in a certain direction. For example, you may develop a game that runs only in Landscape mode. In this case, you can write code to force the direction of the activity to be set to horizontal, which requires the setrequestorientation () method of the activity class. [Java] view plaincopyimport android. content. PM. activityinfo; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // setrequestedorientation (activityinfo. screen_orientation_landscape);} to set it to portrait mode, you can use activityinfo. screen_orientation_portrait constant. In addition to the setrequestorientation () method, you can also set it in the androidmanifest. xml file. [HTML] view plaincopy <? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "net. learn2develop. orientations "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <uses-SDK Android: minsdkversion =" 14 "/> <application Android: icon = "@ drawable/ic_launcher" Android: Label = "@ string/app_name"> <activity Android: Label = "@ string/app_name" Android: Name = ". orientationsactivity "<! -- Set screenorientation --> Android: screenorientation = "Landscape"> <intent-filter> <action Android: Name = "android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> </manifest> in the previous example, the contained activity is set to Landscape mode, the activity is also blocked from being destroyed. This is because the activity is set to a fixed direction. When the device direction changes, the activity will not be destroyed, and the oncreate () method will not be called. The following two values are available for the Android: screenorientation attribute: Portrait -- portrait Mode Sensor -- determines the direction based on the gravity Tester (default)
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.