1.首先通过以下语句设置Activity为无标题和全屏模式:

1 // 设置为无标题栏  
2 requestWindowFeature(Window.FEATURE_NO_TITLE);  
3   
4 // 设置为全屏模式  
5 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
6                      WindowManager.LayoutParams.FLAG_FULLSCREEN);  
7 setContentView(R.layout.main);

2.下面给出xml文件配置,这里我们在res目录下建立layout-land和layout-port目录,相应的layout文件不变,比如main.xml。layout-land是横屏的layout,layout-port是竖屏的layout,其他的不用管模拟器自动寻找
main.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout  
  xmlns:android="http://schemas.android.com/apk/res/android"  
  android:background="@drawable/white"  
  android:orientation="vertical"  
  android:layout_width="fill_parent"  
  android:layout_height="fill_parent"  
>  
  <TextView  
    android:id="@+id/myTextView1"  
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"  
    android:textColor="@drawable/blue"   
    android:text="the portrait"  
  />  
  <Button  
    android:id="@+id/myButton1"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:text="@string/str_button1" />  
</LinearLayout>

这个xml文件需要在上述所说的2个文件夹下都需要放置.

3.获取资源id的view:

mButton01 = (Button) findViewById(R.id.myButton1);  
mTextView01 = (TextView) findViewById(R.id.myTextView1);

4.返回当前显示Activity的显示状态(横屏还是竖屏)

// Return the current requested orientation of the activity

if (getRequestedOrientation() == -1){  
   mTextView01.setText(getResources().getText(R.string.str_err_1001));  
}

5.设置按钮点击监听器

/* 当点击按钮旋转屏幕画面 */  
    mButton01.setOnClickListener(new Button.OnClickListener()  
    {  
      // @Override  
      public void onClick(View arg0)  
      {  
        /* 方法一:重写getRequestedOrientation */  
  
        /* 若无法取得screenOrientation属性 */  
        if (getRequestedOrientation() == -1)  
        {  
          /* 提示无法进行画面旋转功能,因无法判别Orientation */  
          mTextView01.setText(getResources().getText(R.string.str_err_1001));  
        } else  
        {  
          if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)  
          {  
            /* 若当下为横排,则更改为竖排呈现 */  
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
          } else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  
          {  
            /* 若当下为竖排,则更改为横排呈现 */  
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
          }  
        }  
      }  
    });  
  }

6.改变屏幕显示的函数

@Override  
public void setRequestedOrientation(int requestedOrientation)  
{  
  // TODO Auto-generated method stub  
  
  /* 判断要更改的方向,以Toast提示 */  
  switch (requestedOrientation)  
  {  
    /* 更改为LANDSCAPE */  
    case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):  
      mMakeTextToast(getResources().getText(R.string.str_msg1).toString(),  
          false);  
      break;  
    /* 更改为PORTRAIT */  
    case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):  
      mMakeTextToast(getResources().getText(R.string.str_msg2).toString(),  
          false);  
      break;  
  }  
  super.setRequestedOrientation(requestedOrientation);  
}

7.获取屏幕显示状态

@Override  
public int getRequestedOrientation()  
{  
  // TODO Auto-generated method stub  
  
  /* 此重写getRequestedOrientation方法,可取得当下屏幕的方向 */  
  return super.getRequestedOrientation();  
}

8.通过一个Toast来显示屏幕状态

public void mMakeTextToast(String str, boolean isLong)  
 {  
   if (isLong == true)  
   {  
     Toast.makeText(EX05_22.this, str, Toast.LENGTH_LONG).show();  
   } else  
   {  
     Toast.makeText(EX05_22.this, str, Toast.LENGTH_SHORT).show();  
   }  
 }

9.colors.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <drawable name="darkgray">#808080</drawable>  
  <drawable name="white">#FFFFFF</drawable>  
  <drawable name="blue">#0000FF</drawable>  
</resources>

10.strings.xml文件内容如下:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
    <string name="hello">Hello World, ScreenChangeEx</string>  
    <string name="app_name"> ScreenChangeEx </string>  
    <string name="str_button1">按我旋转屏幕</string>  
    <string name="str_err_1001">  
        请在AndroidManifest.xml\n添加android:screenOrientation属性  </string>  
    <string name="str_msg1">旋转为LANDSCAPE</string>  
    <string name="str_msg2">旋转为PORTRAIT</string>  
</resources>

11.AndroidManifest.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
    package="cn.com" android:versionCode="1" android:versionName="1.0.0">  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".ActivityMain" 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>  
</manifest>

注意:

1、不设置Activity的android:configChanges时,切屏会重新调用各个生命周期,切横屏时会执行一次,切竖屏时会执行两次
2、设置Activity的android:configChanges="orientation"时,切屏还是会重新调用各个生命周期,切横、竖屏时只会执行一次
3、设置Activity的android:configChanges="orientation|keyboardHidden"时,切屏不会重新调用各个生命周期,只会执行onConfigurationChanged方法

13.程序整个ActivityMain.java文件如下:

import android.app.Activity;  
import android.content.pm.ActivityInfo;  
import android.os.Bundle;  
import android.view.View;  
import android.view.Window;  
import android.view.WindowManager;  
import android.widget.Button;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class ActivityMain extends Activity  
{  
  private TextView mTextView01;  
  private Button mButton01;  
  
  /** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle savedInstanceState)  
  {  
    super.onCreate(savedInstanceState);  
  
    // 设置为无标题栏  
    requestWindowFeature(Window.FEATURE_NO_TITLE);  
  
    // 设置为全屏模式  
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
        WindowManager.LayoutParams.FLAG_FULLSCREEN);  
    setContentView(R.layout.main);  
  
    mButton01 = (Button) findViewById(R.id.myButton1);  
    mTextView01 = (TextView) findViewById(R.id.myTextView1);  
  
    // Return the current requested orientation of the activity  
    if (getRequestedOrientation() == -1)  
    {  
      mTextView01.setText(getResources().getText(R.string.str_err_1001));  
    }  
  
    /* 当点击按钮旋转屏幕画面 */  
    mButton01.setOnClickListener(new Button.OnClickListener()  
    {  
      // @Override  
      public void onClick(View arg0)  
      {  
        /* 方法一:重写getRequestedOrientation */  
  
        /* 若无法取得screenOrientation属性 */  
        if (getRequestedOrientation() == -1)  
        {  
          /* 提示无法进行画面旋转功能,因无法判别Orientation */  
          mTextView01.setText(getResources().getText(R.string.str_err_1001));  
        } else  
        {  
          if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)  
          {  
            /* 若当下为横排,则更改为竖排呈现 */  
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);  
          } else if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)  
          {  
            /* 若当下为竖排,则更改为横排呈现 */  
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);  
          }  
        }  
      }  
    });  
  }  
  
  @Override  
  public void setRequestedOrientation(int requestedOrientation)  
  {  
    // TODO Auto-generated method stub  
  
    /* 判断要更改的方向,以Toast提示 */  
    switch (requestedOrientation)  
    {  
      /* 更改为LANDSCAPE */  
      case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):  
        mMakeTextToast(getResources().getText(R.string.str_msg1).toString(),  
            false);  
        break;  
      /* 更改为PORTRAIT */  
      case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):  
        mMakeTextToast(getResources().getText(R.string.str_msg2).toString(),  
            false);  
        break;  
    }  
    super.setRequestedOrientation(requestedOrientation);  
  }  
  
  @Override  
  public int getRequestedOrientation()  
  {  
    // TODO Auto-generated method stub  
  
    /* 此重写getRequestedOrientation方法,可取得当下屏幕的方向 */  
    return super.getRequestedOrientation();  
  }  
  
  public void mMakeTextToast(String str, boolean isLong)  
  {  
    if (isLong == true)  
    {  
      Toast.makeText(EX05_22.this, str, Toast.LENGTH_LONG).show();  
    } else  
    {  
      Toast.makeText(EX05_22.this, str, Toast.LENGTH_SHORT).show();  
    }  
  }  
}