Android horizontal and vertical screen switching Solution

Source: Internet
Author: User

During Game Development, some games can only be played on a horizontal screen. Therefore, when mobile phones are erected and placed, the game screen must be kept on a horizontal screen. To do this, you can simply configure it in AndroidManifest. xml. Add this line to android: screenOrientation = "landscape ".

For example, (landscape is horizontal and portrait is vertical ):
Java code

1. <? Xml version = "1.0" encoding = "UTF-8"?>
2. <manifest xmlns: android = "http://schemas.android.com/apk/res/android"
3. package = "com. ray. linkit"
4. android: versionCode = "1"
5. android: versionName = "1.0" type = "codeph" text = "/codeph">
6. <application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
7. <activity android: name = ". Main"
8. android: label = "@ string/app_name"
9. android: screenOrientation = "portrait">
10. <intent-filter>
11. <action android: name = "android. intent. action. MAIN"/>
12. <category android: name = "android. intent. category. LAUNCHER"/>
13. </intent-filter>
14. </activity>
15. <activity android: name = ". GamePlay"
16. android: screenOrientation = "portrait"> </activity>
17. <activity android: name = ". OptionView"
18. android: screenOrientation = "portrait"> </activity>
19. </application>
20. <uses-sdk android: minSdkVersion = "3"/>
21. </manifest>

<? 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" type = "codeph" text = "/codeph">
<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>
<Uses-sdk android: minSdkVersion = "3"/>
</Manifest>

In addition, the Activity is restarted every time the screen is switched on in android. Therefore, you should save the status of the current Activity before the Activity is destroyed and load the configuration when the Activity is created again, in progress, the game will not automatically restart!
When the screen changes to a horizontal screen, the system will call the OnCreate method of the current Activity again. You can put the following method in your OnCreate to check the current direction, then, let your SetContentView load different Layout xml.

If (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_LANDSCAPE ){

Log. I ("info", "landscape ");

}

Else if (this. getResources (). getConfiguration (). orientation = Configuration. ORIENTATION_PORTRAIT ){

Log. I ("info", "portrait ");

}

About screen Switching
First, add the configuration in androidmanifest. xml.
Android: configChanges = "orientation | keyboardHidden | navigation
In this way, the. Activity in the program will not repeatedly call onCreate ()
It does not even call onPause. onResume.
Only one onConfigurationChanged (Configuration newConfig) is called)
This is only when XML is added with configuration options.

If the option is added, the onCreate method will be re-activated for the. Activity method mentioned above.

It is better to select the processing mechanism for configuration changes based on your own needs.

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.