首页 > 解决方案 > 是否可以将状态栏覆盖在活动上,但不能覆盖导航栏

问题描述

我有一个活动,我将 Android 状态栏覆盖在屏幕顶部附近的图片上。我通过继承活动样式Theme.AppCompat.DayNight.NoActionBar然后在以下中执行此操作来实现这一点onCreate()

            getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, 
                                 WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

这样可以在顶部实现透明状态栏,这很好。

问题是,这也使底部的 Android 导航栏透明并覆盖在 Activity 底部附近的控件上,这不是我想要的。我希望导航栏是不透明的,并且在不超过我的控件的情况下占用它的垂直空间,就像在不溢出状态栏的常见活动中一样。

是否可以与状态栏分开控制导航栏的透明度和重叠,而无需任何人工边距或填充?

标签: androidoverlayfullscreenstatusbarnavigationbar

解决方案


如果我理解正确,您的意图是在不触摸导航栏的情况下获得透明的状态栏,如下面的屏幕截图所示。

在此处输入图像描述

就个人而言,我也尝试使用该参数FLAG_LAYOUT_NO_LIMITS而没有达到预期的结果。问题是我得到状态栏和导航栏同时被图形作为按钮“覆盖”。一个临时解决方案是检测导航栏并以编程方式向图形添加填充。这部分没用,因为许多 android 用户使用该adb shell overscan命令隐藏导航栏。这个命令的主要问题是它有效地隐藏了导航栏,但欺骗了系统,因为表示导航栏是否存在的布尔值没有改变。此 API 在 Android 11 中已完全删除。

可以通过玩弄样式来达到预期的效果。 将这些属性添加到您的活动风格中:

<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowTranslucentStatus">true</item>

你会得到这样的风格:

<style name="ExampleThemeStackOverFlow" parent="Theme.MaterialComponents.DayNight.NoActionBar">
    <item name="colorPrimary">@color/colorAccentX</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:navigationBarColor">@color/colorPrimary</item>
    <item name="android:windowLightNavigationBar" tools:targetApi="27">true</item>

    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">true</item>
</style>

推荐阅读