在Lollipop系统中完全透明的状态栏和导航栏

66

我正在尝试制作一个安卓启动器。我希望实现完全透明的状态栏和导航栏,这是我的主题xml文件。

<resources>
    <style name="Theme" parent="android:Theme.Material.Wallpaper.NoTitleBar">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">false</item>
        <item name="android:windowTranslucentNavigation">false</item>
    </style>
</resources>

最后两个项目无法正常工作,在棒棒糖上仍然有阴影。

这就是它的样子(请注意状态栏和导航栏实际上都有阴影): enter image description here

我想要达到的效果(使用Nova Launcher):

enter image description here

如何使状态栏和导航栏变为“透明”的,而不是“半透明”的?

13个回答

0

来自Android R

fun Activity.setTransparentStatusBar() {
    WindowCompat.setDecorFitsSystemWindows(window, false)
    window.statusBarColor = Color.TRANSPARENT
    window.navigationBarColor = Color.TRANSPARENT
}

就是这样


0

您还可以将colorPrimary和colorPrimaryDark的alpha值更改为00,然后将以下内容添加到onCreate方法中:

    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

并且将此添加到您的活动中:

android:fitsSystemWindows="true"

0

根据Arpan Sarkar的答案,只需使用WindowCompat即可减少代码量并避免使用已弃用的定义。

fun Activity.transparentSystemBars(
    behindStatusBarIsLight: Boolean,
    behindNavigationBarIsLight: Boolean,
) {
    // Android 4 is hard to debug and apparently ViewCompat.setOnApplyWindowInsetsListener isn't
    // reporting any value there so let's skip and simplify
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) return

    WindowCompat.setDecorFitsSystemWindows(window, false)

    if (behindStatusBarIsLight || behindNavigationBarIsLight) {
        val insetsController: WindowInsetsControllerCompat =
            WindowCompat.getInsetsController(window, window.decorView)
        if (behindStatusBarIsLight)
            insetsController.isAppearanceLightStatusBars = true
        if (behindNavigationBarIsLight)
            insetsController.isAppearanceLightNavigationBars = true
    }
    
    val isLightStatusBarAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
    val isLightNavigationBarAvailable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
    val shouldStatusBarBeTransparent = !behindStatusBarIsLight || isLightStatusBarAvailable
    val shouldNavigationBarBeTransparent = !behindNavigationBarIsLight || isLightNavigationBarAvailable

    val systemUiScrim = ColorUtils.setAlphaComponent(Color.BLACK, 0x40) // 25% black
    window.statusBarColor = if (shouldStatusBarBeTransparent) Color.TRANSPARENT else systemUiScrim
    window.navigationBarColor = if (shouldNavigationBarBeTransparent) Color.TRANSPARENT else systemUiScrim

    // You may set android:enforceNavigationBarContrast to false in style.xml as doing it in code isn't as effective apparently.
}

在 Material 库中还有 EdgeToEdgeUtils.applyEdgeToEdge,它可以工作但不应该被普通开发者使用,我猜它总有一天会向公众提供。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接