简体   繁体   English

透明状态栏(带可见导航栏)

[英]Transparent status bar (with visible navigation bar)

I know this question has been asked many times, but all of the answers either not working, or use deprecated code:我知道这个问题已经被问过很多次了,但是所有的答案要么不起作用,要么使用不推荐使用的代码:

I want to achieve the same effect as latest google maps app:我想达到与最新的谷歌地图应用程序相同的效果:

在此处输入图像描述

  • Fully transparent status bar (Only status bar. not navigation bar!)完全透明的状态栏(只有状态栏。不是导航栏!)
  • Non deprecated solution不推荐使用的解决方案

WindowCompat.setDecorFitsSystemWindows(window, false) partially working as it also hides the navigation bar also WindowCompat.setDecorFitsSystemWindows(window, false)部分工作,因为它也隐藏了导航栏

Add this to your theme将此添加到您的主题中

<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowLightStatusBar" ns1:targetApi="23">true</item>

And use this method并使用这种方法

     private fun handleStatusBar() {
        //set fullScreen (draw under statusBar and NavigationBar )
            WindowCompat.setDecorFitsSystemWindows(window, false)

            container.setOnApplyWindowInsetsListener { view, insets ->
            val navigationBarHeight = WindowInsetsCompat.toWindowInsetsCompat(insets)
                .getInsets(WindowInsetsCompat.Type.navigationBars()).bottom

            view.updatePadding(bottom = navigationBarHeight)
            WindowInsetsCompat.CONSUMED.toWindowInsets()
        }

        //make statusBar content dark
        WindowCompat.getInsetsController(window, window.decorView)?.isAppearanceLightStatusBars =
            true

        window.statusBarColor = Color.TRANSPARENT
    }

I have achieved similar result by following approach.我通过以下方法取得了类似的结果。

    <style name="Theme.WebImageDownloader" parent="Theme.MaterialComponents.DayNight.NoActionBar">
     
    // make status bar icons colour grey
    <item name="android:windowLightStatusBar">true</item>
    // override the notch area
    <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>
    // make status bar transparent
    <item name="android:windowTranslucentStatus">true</item>
    // make navigation are transparent
    <item name="android:windowTranslucentNavigation">true</item>
   </style>

And set above theme in the manifest并在清单中设置上述主题

 android:theme="@style/Theme.WebImageDownloader">

Further more, i have placed my tool bar in the activity as follows,此外,我已将我的工具栏放在活动中,如下所示,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@android:color/holo_blue_dark"
android:layout_height="match_parent">

// other views 

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="32dp"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/ToolBarStyle" />

</RelativeLayout>

There 'ToolBarStyle', you can have any colour for background , text etc.有“ToolBarStyle”,您可以为背景、文本等设置任何颜色。

To make the StatusBar completely transparent,要使StatusBar完全透明,

First, set its color as transparent in the theme or by code as :首先,在主题中或通过代码将其颜色设置为透明:

//In Theme
<item name="android:statusBarColor">@android:color/transparent</item>
//In Code
window.statusBarColor = android.R.color.transparent

Then, to draw the view behind the StatusBar , use this code:然后,要在StatusBar后面绘制视图,请使用以下代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    window.setDecorFitsSystemWindows(false)
} else {
    window.setFlags(
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
    )
}

You can also play with the boolean property android:windowLightStatusBar , it sets the StatusBar 's text and icon color as black or white, You can do it programmatically as well.您还可以使用boolean属性android:windowLightStatusBar ,它将StatusBar的文本和图标颜色设置为黑色或白色,您也可以以编程方式进行设置。 You can also set color to your NavigationBar if you don't want to hide it.如果您不想隐藏它,您还可以为NavigationBar设置颜色。

Output:输出:

在此处输入图片说明

To make the stats bar transparent: add the below into the app's style themes.xml or sytles.xml :要使统计栏透明:将以下内容添加到应用程序的样式themes.xmlsytles.xml

<item name="android:windowTranslucentStatus" tools:targetApi="kitkat">true</item>

<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent

Then in activity, the used window flags are deprecated as of API level 30, so they can be used till API level 29:然后在活动中,使用的窗口标志从 API 级别 30 开始被弃用,因此它们可以使用到 API 级别 29:

if (Build.VERSION.SDK_INT in 21..29) { 
    window.statusBarColor = Color.TRANSPARENT
    window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    window.decorView.systemUiVisibility =
        SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or SYSTEM_UI_FLAG_LAYOUT_STABLE
}

if (Build.VERSION.SDK_INT >= 30) {
    window.statusBarColor = Color.TRANSPARENT
    // Making status bar overlaps with the activity
    WindowCompat.setDecorFitsSystemWindows(window, false)
}

This is tested on 8 devices/emulators on the range of API level 19 to API level 30.这在 API 级别 19 到 API 级别 30 的范围内在 8 个设备/模拟器上进行了测试。

private fun setStatusBar() {
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        window.setFlags(
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION
        )
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
        window.statusBarColor = Color.TRANSPARENT
        window.navigationBarColor = Color.TRANSPARENT
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
    }

After days of searching, and literally have read all stackoverflow related posts.经过几天的搜索,实际上已经阅读了所有与 stackoverflow 相关的帖子。 I hit to this below settings which worked for me.我点击了以下对我有用的设置。 As I just played around and suddenly ran into it, so I have no idea why it worked.因为我只是在玩,突然遇到了它,所以我不知道它为什么起作用。

SDK : minSdkVersion 28 targetSdkVersion 30 SDK : minSdkVersion 28 targetSdkVersion 30

So, most of the suggestions were using deprecated APIs and most likely like this :因此,大多数建议都使用已弃用的 API,并且很可能是这样的:

 <item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">false</item> <item name="android:statusBarColor">@android:color/transparent</item> <item name="android:navigationBarColor">@android:color/transparent</item> <item name="android:windowLayoutInDisplayCutoutMode">shortEdges</item>

And what i got with those settings: 1我从这些设置中得到了什么: 1

Then it came to that grey status bar which i saw many people, same as me, wanted to get rid of it.然后它来到了那个灰色的状态栏,我看到很多人和我一样想要摆脱它。

 <item name="android:windowTranslucentStatus">false</item>

I disabled that translucentStatus, and handle the display behind status bar using code :我禁用了 translucentStatus,并使用代码处理状态栏后面的显示:

 @Override protected void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_MyApp); getWindow().setDecorFitsSystemWindows(false); super.onCreate(savedInstanceState); setContentView(R.layout.activity_log_in);

I got this result after that : 2之后我得到了这个结果: 2

Lastly, we change the status bar text color:最后,我们更改状态栏文本颜色:

 <item name="android:windowLightStatusBar">true</item>

And it works: 3它有效: 3

:) :)

i do this for my app inside onCreate我在 onCreate 中为我的应用程序执行此操作

with(window) {
        setFlags(
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
            WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
        )
    }

it works for me, and i hope it works for you too.它对我有用,我希望它也对你有用。

For who used a whole day to find how to make the status bar completely transparent.对于谁用了一整天的时间来寻找如何使状态栏完全透明。

Firstly, remove the code like those: as my personal opinion, those setting is easily to mess up everything.首先,删除这样的代码:在我个人看来,这些设置很容易搞砸一切。

This one will make the spinner inside scrollview not working.这将使滚动视图内的微调器不起作用。

getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

 View.setSystemUiVisibility() 

 final View decorView = window.getDecorView();
 decorView.setSystemUiVisibility();

After you clean up the code that you tried before.清理之前尝试过的代码之后。

Add this into the activity OnCreated method.将此添加到活动 OnCreated 方法中。

WindowCompat.setDecorFitsSystemWindows(getWindow(), false);

And add this into your theme file, base on you want to make navigation bar or status bar transparent并将其添加到您的主题文件中,基于您要使导航栏或状态栏透明

<item name="android:navigationBarColor">@android:color/transparent</item>-->
<item name="android:statusBarColor">@android:color/transparent</item>

Add this if you need to setup the status bar with darker text color如果您需要使用较深的文本颜色设置状态栏,请添加此选项

<item name="android:windowLightStatusBar">true</item>

Ref: https://developer.android.com/training/gestures/edge-to-edge参考: https://developer.android.com/training/gestures/edge-to-edge

I have tried all answers with different android versions.. and what worked with me:我已经尝试了不同 android 版本的所有答案.. 什么对我有用:

  • Don't set the android:windowTranslucentStatus as true, it will make a transparent status bar but with dark shadow.不要将 android:windowTranslucentStatus 设置为 true,它将使状态栏透明但带有深色阴影。
// no need to add this.. it is false in default
<item name="android:windowTranslucentStatus">false</item>
  • call this method in your activity to make the status bar transparent even for android 11 and 12在您的活动中调用此方法以使状态栏即使对于 android 11 和 12 也是透明的
public void transparentStatusBar() {
        Window window = getWindow();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            window.setDecorFitsSystemWindows(false);
        } else {
            window.setFlags(
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
                    WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
            );
        }
        window.setStatusBarColor(Color.TRANSPARENT);
}
  • If you found the navigation bar overlays the bottom views.. Just add padding at the bottom and avoid this:)如果您发现导航栏覆盖了底部视图.. 只需在底部添加填充并避免这种情况:)
  • To set the status bar tint black将状态栏设置为黑色
<item name="android:windowLightStatusBar" tools:targetApi="M">true</item>

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

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