Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีปิดการหมุนหน้าจอ (Orientation Change) ใน Android

การปิดการเปลี่ยนทิศทางหน้าจอ (Lock Screen Orientation)

ตัวอย่างนี้สาธิตวิธีล็อกหน้าจอให้อยู่ในทิศทางเดียว (Portrait หรือ Landscape) ทั้งในโค้ด Java และ AndroidManifest.xml

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio เลือก File → New Project และกรอกข้อมูลที่จำเป็น

ขั้นตอนที่ 2: จัดทำ Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    tools:context=".MainActivity"
    android:background="#ecdcf1">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="โหมดแนวตั้งถูกปิดใช้งาน!"
        android:textStyle="bold"
        android:textColor="@color/colorPrimary"
        android:layout_centerVertical="true"/>
</RelativeLayout>

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/MainActivity.java) - ล็อกเป็น Landscape

import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ล็อกให้อยู่ใน Landscape ทุกครั้งที่ Activity สร้าง
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        setContentView(R.layout.activity_main);
    }
}

วิธีที่ 2: ล็อกผ่าน AndroidManifest.xml (แนะนำ)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity 
            android:name=".MainActivity" 
            android:screenOrientation="portrait">  // หรือ "landscape"
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

ตัวเลือก screenOrientation ที่ใช้ได้

  • portrait - แนวตั้ง (ค่าเริ่มต้น)
  • landscape - แนวนอน
  • sensor - ตามเซ็นเซอร์ (auto-rotate)
  • nosensor - ไม่ใช้เซ็นเซอร์ คงทิศทางเดิม
  • user - ตามการตั้งค่าผู้ใช้
  • fullSensor - ใช้เซ็นเซอร์ทั้ง 4 ทิศทาง

การทดสอบ

รันแอปพลิเคชัน ลองหมุนอุปกรณ์ หน้าจอจะคงทิศทางตามที่กำหนดไว้

วิธีปิดการหมุนหน้าจอ (Orientation Change) ใน Android

แนวทางปฏิบัติที่ดีที่สุด

แนะนำให้ใช้ android:screenOrientation ใน Manifest มากกว่า setRequestedOrientation() ในโค้ด เพราะ:

  1. มีผลตั้งแต่ Activity เริ่มต้น (ไม่มีการกระพริบ)
  2. ระบบจัดการได้ดีกว่า
  3. โค้ดสะอาดกว่า แยกการตั้งค่าออกจาก Logic