An effective method for banning vertical screen switching in horizontal screen with Android _android

Source: Internet
Author: User

In Android to let a program's interface always maintain a direction, not with the mobile phone rotation and change the way: As long as the androidmanifest.xml inside the configuration can be.

The android:screenorientation= "Landscape" attribute can be added to the configuration of Androidmanifest.xml activity (which requires an activity that prohibits steering) (landscape is horizontal, Portrait is longitudinal). For example:

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= "http://schemas.android.com/apk/res/" Android "package=" Com.ray.linkit "android:versioncode=" 1 "android:versionname=" 1.0 "> <application and roid:icon= "@drawable/icon" android:label= "@string/app_name" > <activity android:name= ". Main "android:label=" @string/app_name "android:screenorientation=" Portrait "> <intent-filt er> <action android:name= "Android.intent.action.MAIN"/> <category "android:name=" Nt.category.LAUNCHER "/> </intent-filter> </activity> <activity android:name=". GamePlay "android:screenorientation=" Portrait "></activity> <activity android:name=". Optionview "android:screenorientation=" Portrait "></activity> </application> <uses-sdk and
 roid:minsdkversion= "3"/> </manifest>

In addition, every time the screen switch in Android will restart the activity, so should be in the activities before the destruction of the current active state, in the event again create the load configuration, so that the game will not automatically restart!

To avoid restarting the activity when the screen is turned on, you can redefine the orientation in the Androidmanifest.xml file (add android:configchanges= to each activity) keyboardhidden| Orientation "property) and is controlled according to the activity's rewrite onconfigurationchanged (Configuration newconfig) method so that the activity is not restarted when the screen is turned. Instead, it will call the onconfigurationchanged (Configuration newconfig) Hook method. For example:

if (newconfig.orientation==configuration.orientation_landscape) {
 //transverse 
 Setcontentview (r.layout.file_list _landscape); 
} else{
 //Vertical 
  Setcontentview (r.layout.file_list); 
}

In the emulator, you can use the shortcut key F12 or ctrl+f11 to toggle the program's turn screen. Of course, when you start the simulator with the command line, you can use the parameter Emulator.exe-skin hvga-l to start the horizontal screen program directly.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.