天天看點

Android 基礎教程之---動态更改螢幕方向的簡單例子(LANDSCAPE與PORTRAIT)!

大家好,今天要講的是Android手機如何動态手機螢幕方向的,我們當中有可能手機也會有這種功能,當我們手機方向改變時, 螢幕也會跟着改變,在這Android當中是很容易實作的.本節的Demo主要是界面有一個按鈕,當點選時,如果螢幕方向是橫排(PORTRAIT)剛将 螢幕方向更改為豎排(LANDSCAPE),反之依然!我們這裡主要是運用了getRequestedOrientation(),和 setRequestedorientation()兩個方法.但是要利用這兩個方法必須先在AndroidManiefst.xml設定一下螢幕方屬 性,不然程式将不能正常的工作.下面我将分為N個步驟一步一步教你如何實作該Demo.

Step 1:我們建立一個Android工程,命名為ChangeOrientationDemo.

Step 2:設計UI,打開main.xml,将其代碼修改如下,我們這裡隻是增加了一個按鈕,其他什麼都沒有動.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        android:orientation="vertical"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        >

<TextView    

        android:layout_width="fill_parent"    

        android:layout_height="wrap_content"    

        android:text="@string/hello"

        />

<Button

android:id="@+id/bt1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Press me Change Orientation"

/>

</LinearLayout>

Step 3:設計主程式ChangeOrientationDemo.java,修改其代碼如下:

package com.android.test;

import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

public class ChangeOrientationDemo extends Activity {

private Button bt1;

        public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);

                //擷取資源

                bt1 = (Button)findViewById(R.id.bt1);

                //增加按鈕事件

                bt1.setOnClickListener(new Button.OnClickListener(){

     @Override

     public void onClick(View v) {

        //如果是豎排,則改為橫排

        if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)

        {

         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        }

        //如果是橫排,則改為豎排

        else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)

         setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

     }

                });

}

Step 4:在AndroidManifest.xml檔案裡設定預設方向,不然程式不能正常工作哦.代碼如下:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

            package="com.android.test"

            android:versionCode="1"

            android:versionName="1.0">

        <application android:icon="@drawable/icon" android:label="@string/app_name">

                <activity android:name=".ChangeOrientationDemo"

                                    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>

        </application>

        <uses-sdk android:minSdkVersion="3" />

</manifest>    

Step 5:運作程式,效果如下圖:

Android 基礎教程之---動态更改螢幕方向的簡單例子(LANDSCAPE與PORTRAIT)!
Android 基礎教程之---動态更改螢幕方向的簡單例子(LANDSCAPE與PORTRAIT)!

 OK,今天就到這裡,謝謝大家!