Android橫、豎屏幕動態切換(layout-land 和layout-port)

下面是一個例子程序: 
1.首先經過如下語句設置Activity爲無標題和全屏模式: java

Java代碼 
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文件以下: android

Java代碼app

<?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個文件夾下都須要放置. ide

3.獲取資源id的view: 函數

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

4.返回當前顯示Activity的顯示狀態(橫屏仍是豎屏) this

Java代碼
// Return the current requested orientation of the activity  
if (getRequestedOrientation() == -1){  
   mTextView01.setText(getResources().getText(R.string.str_err_1001));  
}  

5.設置按鈕點擊監聽器 spa

Java代碼
/* 當點擊按鈕旋轉屏幕畫面 */  
    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.改變屏幕顯示的函數 code

Java代碼
@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.獲取屏幕顯示狀態 xml

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

8.經過一個Toast來顯示屏幕狀態 blog

Java代碼
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文件內容: 

Java代碼 
<?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文件內容以下: 

Java代碼
<?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文件內容: 

Java代碼
<?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:screenOrientation="landscape">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
</manifest>  

13.程序整個ActivityMain.java文件以下: 

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();  
    }  
  }  
}