Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 動態切換螢幕方向:程式碼與設定一次說明

編輯:關於 Android 程式設計

有些 App 需要提供「鎖定方向」的切換按鈕,例如影片播放器或需要看寬表的報表頁。Android 透過 ActivityInfo 的常數搭配 setRequestedOrientation() 就能做到。

版面配置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/mybtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="改變螢幕方向(目前為直向)" />

    <ImageView
        android:id="@+id/myview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/aa" />
</LinearLayout>

Activity 程式碼

package org.lxh.demo;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Hello extends Activity {

    private Button change = null;
    private ImageView img = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.main);

        this.change = (Button) super.findViewById(R.id.mybtn);
        this.img = (ImageView) super.findViewById(R.id.myview);
        this.change.setOnClickListener(new OnClickListenerImpl());
    }

    private class OnClickListenerImpl implements OnClickListener {
        public void onClick(View arg0) {
            if (Hello.this.getRequestedOrientation()
                    == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
                Hello.this.change.setText("無法進行螢幕方向切換");
            } else if (Hello.this.getRequestedOrientation()
                    == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                Hello.this.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            } else if (Hello.this.getRequestedOrientation()
                    == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
                Hello.this.setRequestedOrientation(
                        ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            }
        }
    }
}

Manifest 設定

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="org.lxh.demo"
    android:versionCode="1"
    android:versionName="1.0">

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

    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />

    <application>
        <activity
            android:name=".Hello"
            android:configChanges="orientation|keyboard"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

configChanges 到底做了什麼

android:configChanges="orientation|keyboard" 的意思是:方向或鍵盤狀態改變時,不要讓系統重啟 Activity,而是回呼 onConfigurationChanged()。這裡有個重要取捨——如果不重啟,就必須自己在 onConfigurationChanged 裡重新量測並刷新版面,否則畫面會停在舊方向的排版上。若你的版面完全靠不同資源目錄(layout-land/layout-port)區隔,反而不該加上這個屬性,讓系統正常重建才對。

另外,CHANGE_CONFIGURATION 這個權限在一般應用上並非必要,它屬於 signature 等級,普通簽章的 App 拿不到;可以移除,不影響功能。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved