value文件夹下:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Base.Theme.xxx" parent="Theme.xxx.DayNight.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:navigationBarColor">@android:color/transparent</item>
</style>
</resources>
activity下:
// 获取Window对象
val window = window
if (window != null) {
// 设置全屏显示,并隐藏状态栏
window.setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
)
// 对于Android 14,可以使用新的API来更好地控制系统栏
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Android 14 (API 34) 的新API调用,根据需要实现
} else {
// 对于Android 14之前的版本,使用旧方法
val decorView = window.decorView
val uiOptions = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)
decorView.systemUiVisibility = uiOptions
}
}