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

Cách kiểm tra Activity đang ở chế độ Landscape (ngang)

Giới thiệu

Bài viết này tương tự bài trước nhưng tập trung vào việc phát hiện chế độ Landscape (ngang). Code cơ bản giống hệt nhau, chỉ khác logic hiển thị.

Code xử lý (MainActivity.kt)

package com.example.checklandscape

import android.content.res.Configuration
import android.os.Bundle
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        checkLandscape()
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        checkLandscape()
    }

    private fun checkLandscape() {
        val isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
        tvOrientation.text = if (isLandscape) {
            "Đang ở chế độ LANDSCAPE (ngang)"
        } else {
            "KHÔNG phải Landscape (đang Portrait)"
        }
    }
}

Manifest (AndroidManifest.xml) - Bắt buộc

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize|screenLayout|keyboardHidden"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvOrientation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Lưu ý thêm

  • Code kiểm tra Landscape/Portrait thực chất là cùng một logic, chỉ khác cách hiển thị thông báo.
  • Trong thực tế, thường dùng chung một hàm updateUIForOrientation() để điều chỉnh layout cho cả 2 chế độ.
  • Với ViewBinding hoặc DataBinding, thay kotlinx.android.synthetic (deprecated).

Ví dụ điều chỉnh layout theo orientation

private fun updateUIForOrientation() {
    val isLandscape = resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
    if (isLandscape) {
        // Layout ngang: 2 cột, font nhỏ hơn
        recyclerView.layoutManager = GridLayoutManager(this, 2)
        tvTitle.textSize = 18f
    } else {
        // Layout dọc: 1 cột, font to hơn
        recyclerView.layoutManager = LinearLayoutManager(this)
        tvTitle.textSize = 24f
    }
}