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

Cách ẩn Status Bar trên Android bằng Kotlin

Giới thiệu

Bài viết hướng dẫn cách ẩn thanh trạng thái (Status Bar - hiển thị giờ, pin, thông báo) để ứng dụng chạy toàn màn hình (fullscreen) bằng Kotlin.

Các bước thực hiện

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

Android Studio > File > New Project.

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:background="#008080"
        android:padding="5dp"
        android:text="TutorialsPoint"
        android:textColor="#fff"
        android:textSize="24sp"
        android:textStyle="bold" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!"
        android:textColor="@android:color/holo_blue_dark"
        android:textSize="24sp"
        android:textStyle="bold" />

</RelativeLayout>

Bước 3: MainActivity.kt - Ẩn Status Bar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Window
import android.view.WindowManager

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // Phải gọi TRƯỚC setContentView
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN
        )
        setContentView(R.layout.activity_main)
        title = "KotlinApp"
    }
}

Bước 4: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.hidestatusbar">

    <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

Chạy app, Status Bar sẽ bị ẩn, ứng dụng chiếm toàn bộ màn hình.

Cách hiện đại (API 30+ / Android 11+)

Từ API 30, nên dùng WindowInsetsController thay vì flag deprecated:

// Trong onCreate, sau setContentView
window.setDecorFitsSystemWindows(false)
window.insetsController?.apply {
    hide(WindowInsets.Type.statusBars())
    // systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE // Tùy chọn: cho phép vuốt để hiện
}

Lưu ý

  • requestWindowFeature(Window.FEATURE_NO_TITLE) cũng ẩn Action Bar/Toolbar nếu theme có. Nếu dùng Theme.MaterialComponents.DayNight.NoActionBar thì không cần dòng này.
  • Để ẩn cả Navigation Bar, thêm WindowInsets.Type.navigationBars() vào hide().
  • Theme fullscreen trong styles.xml (cách khai báo tĩnh):
<style name="AppTheme.Fullscreen" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Áp dụng cho Activity trong Manifest: android:theme="@style/AppTheme.Fullscreen".