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.