Android 5.0 上的 Android 4.4 半透明状态和导航栏样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26474125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:33:42  来源:igfitidea点击:

Android 4.4 translucent Status and Navigation bars style on Android 5.0

androidstatusbarnavigationbarandroid-5.0-lollipop

提问by AxeEffect

On Android 4.4 KitKat you can set the Status and Navigation bars transparent with the android:windowTranslucentStatusand android:windowTranslucentNavigationtheme elements, and then below the bars the app window is extended and a gradient is added. However on Android 5.0 Lollipop this has been changed and now instead of the gradient a solid transparent color is added. Android 5.0 offers the new android:statusBarColorand android:navigationBarColorelements under the new Material theme, but when you try to set these elements to @android:color/transparentthe app window is not extended, and if you use android:windowTranslucentStatusand android:windowTranslucentNavigationthen android:statusBarColorand android:navigationBarColorare ignored.

在 Android 4.4 KitKat 上,您可以使用android:windowTranslucentStatusandroid:windowTranslucentNavigation主题元素将状态栏和导航栏设置为透明,然后在栏下方扩展应用程序窗口并添加渐变。然而,在 Android 5.0 Lollipop 上,这已被更改,现在添加了纯透明颜色而不是渐变。Android 5.0在新的Material 主题下提供了new android:statusBarColorandandroid:navigationBarColor元素,但是当你尝试将这些元素设置到@android:color/transparentapp 窗口时是不会扩展的,如果你使用android:windowTranslucentStatusand android:windowTranslucentNavigationthen android:statusBarColorand 都会android:navigationBarColor被忽略。

Am I missing something described on http://developer.android.com/training/material/theme.html#StatusBar?

我是否遗漏了http://developer.android.com/training/material/theme.html#StatusBar 上描述的内容?

enter image description here

在此处输入图片说明

回答by suckgamony

Set android:windowTranslucentStatusto falseand set android:statusBarColorto @android:color/transparent.

设置android:windowTranslucentStatusfalse并设置android:statusBarColor@android:color/transparent

Then add code below:

然后在下面添加代码:

getWindow().getDecorView().setSystemUiVisibility(
        View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);

If you also want the navigation bar to be translucent, set android:navigationBarColorto @android:color/transparentand combine the flag View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATIONas well.

如果您还希望导航栏是半透明的,也可以设置android:navigationBarColor@android:color/transparent并组合标志View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

I didn't experiment on the navigation bar but it will work.

我没有在导航栏上进行实验,但它会起作用。

回答by Kuldeep Sakhiya

Add below line to your style:

将以下行添加到您的样式中:

<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">true</item>

回答by CODE-REaD

To clarify @suckgamony's answerto this question:

澄清@suckgamony对这个问题的回答

  • Under Lollipop and above, setting android:statusBarColoror android:navigationBarColorto @android:color/transparentwill make the Status Baror Navigation Bar(respectively) completely transparent, unless:
  • android:windowTranslucentStatusor android:windowTranslucentNavigationis set to true, in which case the Status Baror Navigation Bar(respectively) is set to the solid transparent color @AxeEffect describes (again, under Lollipop and above);
  • android:statusBarColorand android:navigationBarColormay only be used with Android version 21 (Lollipop 5.0) or higher. As described in the referred to answer, android:windowTranslucentStatusor android:windowTranslucentNavigationwhen used with Kitkat provide transparent gradients rather than full transparency.
  • 下棒棒糖和上面,设置android:statusBarColorandroid:navigationBarColor@android:color/transparent将使状态栏导航栏(分别)是完全透明的,除非:
  • android:windowTranslucentStatusandroid:windowTranslucentNavigation设置为 true,在这种情况下,状态栏导航栏(分别)设置为@AxeEffect 描述的纯透明颜色(同样,在 Lollipop 及以上);
  • android:statusBarColor并且android:navigationBarColor只能与 Android 版本 21 (Lollipop 5.0) 或更高版本一起使用。如参考答案中所述android:windowTranslucentStatusandroid:windowTranslucentNavigation与 Kitkat 一起使用时提供透明渐变而不是完全透明。