Android横竖屏切换的解决方法

package com.cn;

import android.app.Activity;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class LayoutTest extends Activity {
   
private static final String tag = "LayoutTest";
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        Log.e(tag, "********test********");
       // LOGV("********test********");
       
        if(this.getResources()
.getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE){
Log.e(tag, "^^^^^^^^^^this is a landscape *************");
        }
      //  TextView tx = (TextView)findViewById(R.id.txt);
     //   tx.setKeepScreenOn(true);
      //  if(this.getResources())
       
        setContentView(R.layout.main);
    }
   
    @Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);

if(this.getResources()
.getConfiguration()
.orientation == Configuration.ORIENTATION_PORTRAIT){
Log.e(tag, "********this is a portrait");
}else if(this.getResources()
.getConfiguration()
.orientation == Configuration.ORIENTATION_LANDSCAPE){
Log.e(tag, "this is a landscape *************");
}


}

   
}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.cn"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LayoutTest"
                  android:label="@string/app_name"
                 
                  android:configChanges="orientation|keyboardHidden" >
            <!--   android:screenOrientation="landscape"> -->                                                                    
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest>



在开发游戏的时候,有些游戏是只能横屏玩的,所以手机竖立放置的时候,要保持游戏画面依然横屏。要做到这个要求其实很简单,在AndroidManifest.xml里面配置一下就可以了。加入这一行android:screenOrientation="landscape"。

例如(landscape是横向,portrait是纵向):

java代码

   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"> 
   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> 

另外,android中每次屏幕的切换动会重启Activity,所以应该在Activity销毁前保存当前活动的状态,在Activity再次Create的时候载入配置,那样,进行中的游戏就不会自动重启了!

你可能感兴趣的:(游戏,android,xml,OS,活动)