Android prohibits horizontal screen switch program code

Source: Internet
Author: User

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 a vertical screen). For example:

The code is as follows Copy Code

<?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 android:icon= "@drawable/icon" android:label= "@string/app_name" >
<activity android:name= ". Main "
Android:label= "@string/app_name"
android:screenorientation= "Portrait" >
<intent-filter>
<action android:name= "Android.intent.action.MAIN"/>
<category android:name= "Android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name= ". GamePlay "
android:screenorientation= "Portrait" ></activity>
<activity android:name= ". Optionview "
android:screenorientation= "Portrait" ></activity>
</application>
&LT;USES-SDK android: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, the callback method called Onconfigurationchanged (Configuration newconfig) is invoked. For example

The code is as follows Copy Code

if (Newconfig.orientation==configuration.orientation_landscape) {
Horizontal screen
Setcontentview (R.layout.file_list_landscape);
}else{
Vertical screen
Setcontentview (r.layout.file_list);
}

Note that if your screen layout is not the same, then use onconfigurationchanged will cause the layout does not change after the switch, so this setting is carefully used as well.
.

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.