android - 是否可以将状态栏覆盖在活动上,但不能覆盖导航栏
问题描述
我有一个活动,我将 Android 状态栏覆盖在屏幕顶部附近的图片上。我通过继承活动样式Theme.AppCompat.DayNight.NoActionBar
然后在以下中执行此操作来实现这一点onCreate()
:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
这样可以在顶部实现透明状态栏,这很好。
问题是,这也使底部的 Android 导航栏透明并覆盖在 Activity 底部附近的控件上,这不是我想要的。我希望导航栏是不透明的,并且在不超过我的控件的情况下占用它的垂直空间,就像在不溢出状态栏的常见活动中一样。
是否可以与状态栏分开控制导航栏的透明度和重叠,而无需任何人工边距或填充?
解决方案
如果我理解正确,您的意图是在不触摸导航栏的情况下获得透明的状态栏,如下面的屏幕截图所示。
就个人而言,我也尝试使用该参数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>
推荐阅读
- windows - Windows 10 批处理文件 - IF 使用情况
- php - 从另一个表 MySQL 中获取名称
- python - 在 pymongo 中使用 Find
- osgi - 如何以编程方式从 Karaf 功能中排除捆绑包
- java - 正则表达式由“:{在java中分割?
- php - 邮件未使用 laravel 中的 SendsPasswordResetEmails 类发送忘记密码链接
- opengl - OpenGL:如何最小化绘图?
- powerbi - power bi计算sameperiodlastyear问题
- matlab - 如何在 for 循环中绘图?MATLAB
- qt - 将模型附加到 VXYModelMapper