Android在状态栏后面绘制,但不在导航栏后面 [英] Android Draw behind Status bar but not behind the navigation bar

查看:125
本文介绍了Android在状态栏后面绘制,但不在导航栏后面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种状态栏是完全透明而不是半透明的,并且导航栏保持不变.

I'm looking into a way where the status bar is completely transparent, not translucent and where the Navigation Bar is left untouched.

我能得到的最接近的是使用标志

Closest I can get is to use the flag

WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS

,但这也位于导航栏的后面.

but this draws behind the Navigation Bar as well.

背景是使这个技巧变得棘手的原因,它是一个细微的渐变,当我将状态栏颜色设置为渐变的开始时,它看起来几乎正确,但是在屏幕顶部有一条细线.

The background is what is making this tricky, it is a subtle gradient, and when I set the status bar color to the start of the gradient, it looks almost right, but has a subtle line across the top of the screen.

是否有一种很好的方法可以使活动适合窗口,仅在顶部,但如果在底部有导航栏,就不要管它了吗?

Is there a good way to fit the activity to the window, only at the top but if there is a navigation bar at the bottom, leave it alone?

推荐答案

一种好的方法是此答案中的方法1.

A good approach is Method One from this answer.

要获得完全透明的状态栏,您必须使用 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>

我猜您尝试过此操作或类似操作.如果这不起作用,请确保您的根元素是FrameLayout.

I'm guessing you've tried this or something similar. If that's not working, make sure your root element is a FrameLayout.

这篇关于Android在状态栏后面绘制,但不在导航栏后面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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