android 狀態(tài)欄透明效果

測試設(shè)備:vivo android10,小米 android13
使用主題:Theme.Material3.DayNight.NoActionBar
Activity父類:AppCompatActivity
布局文件需注意添加:android:fitsSystemWindows="true",完整的布局文件在下文
參考文檔:fitsSystemWindows 參考文檔

window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT

里面過時api的處理

        // 過時,官方文檔說使用 window.statusBarColor = Color.TRANSPARENT,
        // https://developer.android.google.cn/reference/kotlin/android/view/WindowManager.LayoutParams?hl=en#flag_translucent_status
        // 由于測試手機是 TIRAMISU,為保險就使用 TIRAMISU
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
            window.clearFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                        or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
            )
        }
        // SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN:https://developer.android.google.cn/reference/kotlin/android/view/View?hl=en#system_ui_flag_layout_fullscreen
        // SYSTEM_UI_FLAG_LAYOUT_STABLE 未提供相應(yīng)替代方案,不過不用好像也沒問題:https://developer.android.google.cn/reference/kotlin/android/view/View?hl=en#system_ui_flag_layout_stable
        // 由于測試手機是 TIRAMISU,為保險就使用 TIRAMISU
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            window.setDecorFitsSystemWindows(false)
        } else {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT

懷疑里面是否有冗余的代碼調(diào)用,測試后發(fā)現(xiàn)可簡化為:

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            window.setDecorFitsSystemWindows(false)
        } else {
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
        }
        window.statusBarColor = Color.TRANSPARENT

注意:如果使用上面這段代碼,并且在主題里面設(shè)置了 <item name="android:windowTranslucentStatus">true</item> 后,在安android10上的狀態(tài)欄是半透明的,而在android13上是全透明。Translucent 本意就是半透明,但是 windowTranslucentStatus 在android13上的效果是全透明。

如果不介意部分手機(android13以下)狀態(tài)欄是半透明的話,僅配置主題就好了,比如:

    <style name="Base.Theme.Docker" parent="Theme.Material3.DayNight.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

android:fitsSystemWindows="true"的作用,測試后推斷所得

  • 透明狀態(tài)欄的情況下,確保當(dāng)前控件的位置在最頂部
  • 確保內(nèi)部控件,不被狀態(tài)欄擋?。ú还苁欠袷窃谕该鳡顟B(tài)欄的情況下)

據(jù)說該屬性在一些控件不生效,我測試的布局文件是(沒錯,fitsSystemWindows屬性放在ImageView里面的):

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

    <android.opengl.GLSurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <ImageView
        android:id="@+id/moreBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_dialog_dialer"
        android:fitsSystemWindows="true"
        android:layout_marginEnd="10dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="bottom|center_horizontal">

        <Button
            android:id="@+id/editBtn"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:text="修改參數(shù)" />

        <Button
            android:id="@+id/applyBtn"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="設(shè)置為壁紙"
            android:layout_marginBottom="50dp"/>

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容