測試設(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>