Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Khóa hướng màn hình (ignore orientation change) trên Android

Giới thiệu

Ví dụ này minh họa cách buộc ứng dụng chỉ hiển thị ở chế độ dọc (portrait) và bỏ qua thay đổi hướng màn hình (landscape) bằng setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT).

Bước 1: Tạo dự án mới

Tạo project Android Studio: File → New Project.

Bước 2: Layout (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Bước 3: MainActivity - Khóa orientation

package com.app.sample;

import androidx.appcompat.app.AppCompatActivity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Buộc chỉ portrait
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

Bước 4: Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.app.sample">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <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">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Chạy ứng dụng

Xoay thiết bị ngang, màn hình vẫn giữ nguyên portrait, không reload activity.

Các chế độ orientation khác

  • SCREEN_ORIENTATION_LANDSCAPE: Buộc ngang.
  • SCREEN_ORIENTATION_SENSOR: Tự xoay theo cảm biến (mặc định).
  • SCREEN_ORIENTATION_USER: Theo cài đặt người dùng (tự xoay nếu bật auto-rotate).
  • SCREEN_ORIENTATION_LOCKED (API 18+): Khóa ở hướng hiện tại.
  • SCREEN_ORIENTATION_FULL_SENSOR: Cho phép xoay 4 chiều (ngược cũng được).

Cách khai báo trong Manifest (thay vì code)

<activity
    android:name=".MainActivity"
    android:screenOrientation="portrait">
</activity>

Cách này hiệu quả hơn vì áp dụng ngay từ khi activity tạo, tránh nháy màn hình.

Xử lý configuration change thủ công

Nếu muốn tự xử lý xoay màn hình mà không recreate activity, thêm vào manifest:

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
</activity>

Sau đó override onConfigurationChanged() trong Activity.