简体   繁体   English

Android透明状态栏和自定义视图插图

[英]Android transparent status bar and custom view insets

I want to draw my layout behind transparent status bar. 我想在透明状态栏后面绘制布局。 I'm using conductor's controllers for implementing my app's screens, all of them have white status bar but one need it to be fully transparent. 我正在使用指挥的控制器来实现我的应用程序的屏幕,它们都具有白色状态栏,但是其中一个需要它是完全透明的。 I can't use windowTranslucentStatus flag when entering this controller because it causes my layouts to jump a little bit when controller enters and exists the screen. 进入此控制器时,我不能使用windowTranslucentStatus标志,因为当控制器进入并存在屏幕时,它会使我的布局略微跳动。 I think that custom window insets could help to have one controller to be drawn behind status bar without layout's jumping but I can't figure out how to it. 我认为自定义窗口插图可以帮助在状态栏后面绘制一个控制器,而不会跳转布局,但是我不知道该怎么做。 Could anyone help me with this please? 有人可以帮我吗?

Use android:fitsSystemWindows="true" in the root view of your layout. 在布局的根视图中使用android:fitsSystemWindows="true"

What does fitsSystemWindows do? fitsSystemWindows什么作用?

System windows are the parts of the screen where the system is drawing either non-interactive (in the case of the status bar) or interactive (in the case of the navigation bar) content. 系统窗口是系统绘制非交互式(对于状态栏而言)还是交互式(对于导航栏而言)内容的屏幕部分。 Most of the time, your app won't need to draw under the status bar or the navigation bar, but if you do: you need to make sure interactive elements (like buttons) aren't hidden underneath them. 大多数情况下,您的应用无需在状态栏或导航栏下绘制,但如果这样做,则需要确保交互元素(如按钮)没有隐藏在它们的下方。 That's what the default behavior of the android:fitsSystemWindows=“true” attribute gives you: it sets the padding of the View to ensure the contents don't overlay the system windows. 这就是android:fitsSystemWindows =“ true”属性的默认行为:它设置了View的padding以确保内容不会覆盖系统窗口。

Source 资源

To do that I would recommend you to hide the status bar and enable full screen window . 为此,我建议您隐藏状态栏并启用全屏窗口 Try below, it works. 试试下面,它可以工作。

Step 1: Create theme in style.xml 步骤1:在style.xml中创建主题

<style name="FullScreen" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowFullscreen">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

Step 2: Apply the theme for activity in manifest file, can also apply theme at the application level too. 步骤2:在清单文件中为活动应用主题,也可以在应用程序级别应用主题。 For example have added in activity level. 例如已添加活动级别。

<activity
    android:name=".MainActivity"
    android:theme="@style/FullScreen" />

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

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