Lollipop : 在 statusBar 后面绘制,颜色设置为透明 [英] Lollipop : draw behind statusBar with its color set to transparent

查看:19
本文介绍了Lollipop : 在 statusBar 后面绘制,颜色设置为透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的状态栏颜色设置为透明,仅在我的主题中使用以下行:

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 属性,但不想使用此属性,因为它会忽略设置为透明的 statusBar 的颜色.

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.

推荐答案

方法一:

要实现完全透明的状态栏,您必须使用 statusBarColor,它仅适用于 API 21 及更高版本.windowTranslucentStatus 在 API 19 及更高版本上可用,但它为状态栏添加了有色背景.但是,设置 windowTranslucentStatus 确实实现了将 statusBarColor 更改为透明所做的一件事:它设置了 SYSTEM_UI_FLAG_LAYOUT_STABLESYSTEM_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 中.这是我用来为 几个月前的 Android Police 文章.

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 扩展的类更改为您喜欢的任何其他类型的 Layout 类来完全避免这种情况.它会工作得很好.

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.

这篇关于Lollipop : 在 statusBar 后面绘制,颜色设置为透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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