且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

完全透明的导航栏,但它不应该影响 Android 上的状态栏

更新时间:2023-01-06 09:40:26

我觉得你想在状态栏和导航后面绘制 View,你可以用新的 WindowInsetterAPI 来设置填充与 System Inset 的高度(包括状态栏)和导航栏).

I think you want to draw View behind the status bar and navigation, You can do it with the new WindowInsetterAPI to set padding with System Inset's height (including status bar and navigation bar).

首先组合这些标志:SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN、SYSTEM_UI_FLAG_LAYOUT_STABLE、SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

First combining these flags: SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, SYSTEM_UI_FLAG_LAYOUT_STABLE, SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

示例:

activity.window.decorView.apply {
            systemUiVisibility =
                systemUiVisibility or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        }

然后你需要你的状态栏透明颜色并设置为深色以便用户可以看到它

Then you need your status bar transparent color and set to dark so the user can see it

<item name="android:statusBarColor">@android:color/transparent</item>    
// Set status bar color to dark 
<item name="android:windowLightStatusBar">true</item>
// Optionally if you also want transparent navigation bar
<item name="android:navigationBarColor">@android:color/transparent</item>

因为您在状态栏后面绘制,所以您的顶视图和底部需要填充状态栏的高度以使其对用户可见(但如果您希望您的图像位于状态后面,请不要对其应用填充).

Because you are drawing behind the status bar so your top view and bottom need padding the status bar height to make it visible to the user (but if you want your image laid behind status don't apply padding to it).

要进行填充,您需要通过以下代码收听 WindowInsets:

To make padding you need to listen to WindowInsets by the following code:

ViewCompat.setOnApplyWindowInsetsListener(view) { v, inset ->
        val systemInsets = inset.systemWindowInsets
        v.setPadding(v.paddingLeft, systemInsets.top,
            v.paddingRight, systemInsets.bottom);
        inset
    }

为了减少样板 WindowInset 侦听器代码,您可以使用 Insetter,只需通过将 padding 直接应用于 XML数据绑定.

To reduce boilerplate WindowInset listener code, you can use Insetter, just apply padding to directly XML through Data Binding.