棒棒糖:画背后的状态栏。它的颜色设置为透明 [英] Lollipop : draw behind statusBar with its color set to transparent

查看:148
本文介绍了棒棒糖:画背后的状态栏。它的颜色设置为透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只在我的主题以下行设置我的状态栏。颜色为透明的,棒棒堂:

I have set my statusBar color to transparent for Lollipop only with the following line in my theme :

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

现在我需要绘制它的身后,但我不能得到它后面的任何视图中绘制。我知道如何使用 windowTranslucentStatus 属性做到这一点,但不想使用此属性,因为它就会忽略状态栏设置为透明的颜色。

Now I need to draw behind it, but I can't get any view draw behind it. I know how to do it with the windowTranslucentStatus property, but don't want to use this property since it will then ignore the color of the statusBar set to transparent.

推荐答案

方法1:

要实现完全透明的状态栏,你必须使用 statusBarColor ,它仅适用于API 21以上。 windowTranslucentStatus 可在API为19及以上,但它增加了一个有色的背景状态栏。但是,设置的 windowTranslucentStatus 确实实现了一件事,改变 statusBarColor 以透明不会:它集SYSTEM_UI_FLAG_LAYOUT_STABLE 和SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN标志。要获得同样的效果,最简单的方法是手动设置这些标志,能有效禁止了Android的布局系统所施加的插图,并让你自生自灭吧。

To achieve a completely transparent status bar, you have to use statusBarColor, which is only available on API 21 and above. windowTranslucentStatus is available on API 19 and above, but it adds a tinted background for the status bar. However, setting windowTranslucentStatus does achieve one thing that changing statusBarColor to transparent does not: it sets the SYSTEM_UI_FLAG_LAYOUT_STABLE and SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN flags. The easiest way to get the same effect is to manually set these flags, which effectively disables the insets imposed by the Android layout system and leaves you to fend for yourself.

您在您的onCreate方法调用这一行:

You call this line in your onCreate method:

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

此外,请务必设置透明度/res/values​​-v21/styles.xml:

Be sure to also set the transparency in /res/values-v21/styles.xml:

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

也可以设置透明度编程方式:

Or set the transparency programmatically:

getWindow().setStatusBarColor(Color.TRANSPARENT);

好的一面这种方法是相同的布局和设计,也可以通过交易出去的透明状态栏的有色半透明的状态条上使用API​​ 19。

The good side to this approach is that the same layouts and designs can also be used on API 19 by trading out the transparent status bar for the tinted translucent status bar.

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

方法2:

如果你只需要画,而不是定位背后景色的背景图片在你的状态栏,这可以通过您的活动的主题背景简单地设置所需的图像,并设置状态栏透明度完成,如图在方法#1。这是我用来创建屏幕截图的<一个方法href="http://www.androidpolice.com/2014/07/04/android-l-spotlight-colored-transparent-status-bars-give-apps-even-control-design/">Android警方文章,从几个月前。

If you only need to paint a background image under your status bar, instead of positioning a view behind it, this can be done by simply setting the background of your activity's theme to the desired image and setting the status bar transparency as shown in method #1. This was the method I used to create the screenshots for the Android Police article from a few months ago.

方法3:

如果你要忽略标准体系插图的一些布局,同时保持它们在其他国家工作,唯一可行的办法来做到这一点是与常联系的 ScrimInsetsFrameLayout 类。当然,一些在这个类做的事情是没有必要的所有场景。例如,如果你不打算使用合成状态栏覆盖,只需注释掉一切都在init()方法,也懒得添加任何的attrs.xml文件。我已经看到了这种方法的工作,但我想你会发现,它带来的可能是大量的工作的一些其他问题得到解决。

If you've got to ignore the standard system insets for some layouts while keeping them working in others, the only viable way to do it is to work with the often linked ScrimInsetsFrameLayout class. Of course, some of the things done in that class aren't necessary for all scenarios. For example, if you don't plan to use the synthetic status bar overlay, simply comment out everything in the init() method and don't bother adding anything to the attrs.xml file. I've seen this approach work, but I think you'll find that it brings some other implications that may be a lot of work to get around.

我也看到了你反对包装多种布局。在包装的另一个,其中两个已经match_parent的高度和宽度内的一个布局的情况下,对性能的影响是过于琐碎操心。无论如何,你可以通过改变它的FrameLayout扩展的类你喜欢的任何其他类型的布局类的完全避免这种情况。它会工作得很好。

I also saw that you're opposed to wrapping multiple layouts. In the case of wrapping one layout inside of another, where both have match_parent for height and width, the performance implications are too trivial to worry about. Regardless, you can avoid that situation entirely by changing the class it extends from FrameLayout to any other type of Layout class you like. It will work just fine.

这篇关于棒棒糖:画背后的状态栏。它的颜色设置为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆