"android:windowTranslucentNavigation" 的意外行为
Unexpected behaviour of "android:windowTranslucentNavigation"
我第一次遇到这种奇怪的行为是在我目前正在开发的主应用程序中。为了调试这个问题,我创建了一个测试应用程序。
测试应用只有一个 activity MainActivity 扩展 Activity。
这个 activity 的主题是 AppTheme.NoActionBar 在 styles.xml
中声明
<style name="AppTheme">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme.Base">
<item name="android:statusBarColor">#FFFFFF</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="AppTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
有了这个输出看起来像这样 图片 1
这不是我所期望的,看看状态栏和工具栏是如何合并在一起的。
但是当我删除这行代码时
<item name="android:windowTranslucentNavigation">true</item>
这就是我最终得到的
请注意,这次工具栏和状态栏没有合并,而是我们有状态栏和它下面的工具栏。
我无法理解 Image 1 中的布局是如何绘制在状态栏下的
我知道这个问题会让很多阅读本文的人感到困惑。但是如何添加属性
<item name="android:windowTranslucentNavigation">true</item>
与状态栏无关的影响。
我的测试设备是 android 运行 Pie
好的,经过一番研究,我发现
android:windowTranslucentNavigation=true
不仅使导航栏半透明,而且还允许布局绘制在状态栏后面。
问题是,如果我们想要彩色导航栏(适用于 Lollipop 及更高版本)而不是半透明的,我们需要删除该行,这样我们将无法在状态栏后面绘制没有了。
解决方法:
声明styles.xml(v21)并设置导航栏颜色
<item name="android:navigationBarColor">@color/red</item>
现在,由于此 styles.xml 文件将仅用于 API >= 21 的设备,我们需要确保布局也绘制在 API 的状态栏后面>= 21 因此,在 activity 的 onCreate 中的 setContentView
之前添加以下行
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) // Checks the API level of the device
{
getWindow()
.getDecorView();
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
结论:
对于设备 (API < 21) android:windowTranslucentNavigation=true 将使导航栏半透明,布局将绘制在状态栏后面,对于设备 ( API >= 21) 我们可以在不破坏UI
的情况下修改导航栏的颜色
我第一次遇到这种奇怪的行为是在我目前正在开发的主应用程序中。为了调试这个问题,我创建了一个测试应用程序。 测试应用只有一个 activity MainActivity 扩展 Activity。 这个 activity 的主题是 AppTheme.NoActionBar 在 styles.xml
中声明 <style name="AppTheme">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar" parent="AppTheme.Base">
<item name="android:statusBarColor">#FFFFFF</item>
<item name="android:windowTranslucentNavigation">true</item>
</style>
<style name="AppTheme.Base" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
有了这个输出看起来像这样
但是当我删除这行代码时
<item name="android:windowTranslucentNavigation">true</item>
这就是我最终得到的
我知道这个问题会让很多阅读本文的人感到困惑。但是如何添加属性
<item name="android:windowTranslucentNavigation">true</item>
与状态栏无关的影响。
我的测试设备是 android 运行 Pie
好的,经过一番研究,我发现
android:windowTranslucentNavigation=true
不仅使导航栏半透明,而且还允许布局绘制在状态栏后面。
问题是,如果我们想要彩色导航栏(适用于 Lollipop 及更高版本)而不是半透明的,我们需要删除该行,这样我们将无法在状态栏后面绘制没有了。
解决方法:
声明styles.xml(v21)并设置导航栏颜色
<item name="android:navigationBarColor">@color/red</item>
现在,由于此 styles.xml 文件将仅用于 API >= 21 的设备,我们需要确保布局也绘制在 API 的状态栏后面>= 21 因此,在 activity 的 onCreate 中的 setContentView
之前添加以下行if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) // Checks the API level of the device
{
getWindow()
.getDecorView();
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE |
View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}
结论: 对于设备 (API < 21) android:windowTranslucentNavigation=true 将使导航栏半透明,布局将绘制在状态栏后面,对于设备 ( API >= 21) 我们可以在不破坏UI
的情况下修改导航栏的颜色