简体   繁体   English

完全透明的导航栏,但不应该影响 Android 上的状态栏

[英]Completely transparent navigation bar but it shouldn't affect the status bar on Android

I need a screen that has a completely transparent navigation bar but the status bar should have colorAccent.我需要一个具有完全透明导航栏但状态栏应该有 colorAccent 的屏幕。

I tried different solutions but none of them work.我尝试了不同的解决方案,但没有一个有效。

1- Tried to change color with XML 1-尝试用 XML 改变颜色

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/zingat_blue_text</item>
    <item name="colorPrimaryDark">@color/zingat_blue_text</item>
    <item name="colorAccent">@color/zingat_blue_text</item>
    <item name="android:colorBackground">@android:color/white</item>
</style>

<style name="NoActionBar" parent="AppTheme">
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

When I tried code above, the navigation bar looks like picture below.当我尝试上面的代码时,导航栏如下图所示。 That is not transparent那不透明

在此处输入图像描述

2- Tried to change windowTranslucentNavigation 2-试图改变 windowTranslucentNavigation

I tried to set android:windowTranslucentNavigation = true and android:windowTranslucentStatus = false like below.我尝试设置 android:windowTranslucentNavigation = true 和 android:windowTranslucentStatus = false 如下所示。

<style name="NoActionBar" parent="AppTheme">
    <item name="android:navigationBarColor">@android:color/transparent</item>
    <item name="android:windowTranslucentStatus">false</item>
    <item name="android:windowTranslucentNavigation">true</item>// CHANGED THIS LINE
</style>

In this case, both status bar and navigation bar affecting and it shows like picture below.在这种情况下,状态栏和导航栏都会影响,如下图所示。 Navigation bar seem a little bit transparent but even I set windowTranslucentStatus=false it has transparent background the Toolbar show bottom of status bar.导航栏看起来有点透明,但即使我设置 windowTranslucentStatus=false 它也有透明背景,工具栏显示状态栏的底部。

在此处输入图像描述

在此处输入图像描述

3- Tried to java code 3-尝试使用 java 代码

Window window = getWindow();
window.setFlags( WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

In this cas the navigation bar looks fully transparent but the status bar also looks transparent and the toolbar slide-up bottom of the status bar like solution 2.在这种情况下,导航栏看起来完全透明,但状态栏也看起来透明,并且工具栏像解决方案 2 一样向上滑动状态栏的底部。

I tried solutions below, none of them work我尝试了以下解决方案,它们都不起作用

Thanks any advance.感谢任何提前。

I think you want to draw View behind the status bar and navigation, You can do it with the new WindowInsetterAPI to set padding with System Inset's height (including status bar and navigation bar).我认为您想在状态栏和导航后面绘制视图,您可以使用新的 WindowInsetterAPI 来设置填充系统插入的高度(包括状态栏和导航栏)。

First combining these flags: SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN, SYSTEM_UI_FLAG_LAYOUT_STABLE, SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION首先结合这些标志:SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN、SYSTEM_UI_FLAG_LAYOUT_STABLE、SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

Example:例子:

activity.window.decorView.apply {
            systemUiVisibility =
                systemUiVisibility or
                        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or
                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE or
                        View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
        }

Then you need your status bar transparent color and set to dark so the user can see it然后你需要你的状态栏透明颜色并设置为深色以便用户可以看到它

<item name="android:statusBarColor">@android:color/transparent</item>    
// Set status bar color to dark 
<item name="android:windowLightStatusBar">true</item>
// Optionally if you also want transparent navigation bar
<item name="android:navigationBarColor">@android:color/transparent</item>

Because you are drawing behind the status bar so your top view and bottom need padding the status bar height to make it visible to the user (but if you want your image laid behind status don't apply padding to it).因为您在状态栏后面绘制,所以您的顶视图和底部需要填充状态栏高度以使其对用户可见(但如果您希望将图像放在状态后面,请不要对其应用填充)。

To make padding you need to listen to WindowInsets by the following code:要进行填充,您需要通过以下代码收听 WindowInsets:

ViewCompat.setOnApplyWindowInsetsListener(view) { v, inset ->
        val systemInsets = inset.systemWindowInsets
        v.setPadding(v.paddingLeft, systemInsets.top,
            v.paddingRight, systemInsets.bottom);
        inset
    }

To reduce boilerplate WindowInset listener code, you can use Insetter , just apply padding to directly XML through Data Binding.要减少样板 WindowInset 侦听器代码,您可以使用Insetter ,只需通过数据绑定直接将填充应用于 XML 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM