首页 > 解决方案 > 带有 BottomSheetDialog 的半透明导航栏

问题描述

BottomSheetDialog当我边到边时,我正在为 targetSdk=30 上的正确导航栏着色而战。我在这里通过代码设置了灯光导航栏选项:

override fun onAttachedToWindow() {
    super.onAttachedToWindow()

    val window = window
        ?: error("window not attached?!")
    val sheetView = findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)
        ?: error("bottom sheet design not found")

    WindowInsetsControllerCompat(window, sheetView).apply {
        isAppearanceLightNavigationBars = true
    }

    ViewCompat.setOnApplyWindowInsetsListener(sheetView) { _, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        sheetView.updatePadding(
            bottom = insets.bottom
        )
        sheetView.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            bottomMargin = -insets.bottom
        }
        WindowInsetsCompat.CONSUMED
    }
}

在我的主题中,我有这个

<style name="BottomSheetDialogTheme" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
    <item name="enableEdgeToEdge">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:navigationBarColor">@android:color/transparent</item>
</style>

正如预期的那样,这会产生一个完全透明的导航栏(毕竟导航栏的颜色是透明的)。但是,当我尝试使导航栏半透明时,即通过给它“#40ffffff”的颜色或通过设置android:windowTranslucentNavigation,导航栏保持透明,没有应用稀松布/背景保护。我究竟做错了什么?

标签: androidtranslucencyandroid-bottomsheetdialogandroid-navigation-bar

解决方案


推荐阅读