机器人:fitsSystemWindows不工作机器人、工作、fitsSystemWindows

2023-09-08 10:01:59 作者:孤心大叔.

在我的主题我定义了以下规则绘制状态栏后面我的看法:

In my theme I defined the following rules to draw my views behind the status bar:

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

和在活动(的onCreate):

And in the Activity (onCreate):

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

然后,使用是应该的状态栏,这是工作的后面画一个吧codescanner我查看即时消息。但是,当我套用的android:fitsSystemWindows 的任何儿童观,他们没有得到自己的位置进行调整。它的工作时,我把它应用到根元素,虽然。

Then, in my View im using a Barcodescanner that is supposed to draw behind the status bar, which is working. However when I apply the android:fitsSystemWindows to any child view they don't get their position adjusted. It's working when I apply it to the root element, though.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">

    <my.Scanner
        android:id="@+id/scanner"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <FrameLayout android:layout_width="match_parent"
                 android:layout_height="match_parent"
                 android:fitsSystemWindows="true"> <!-- Should receive a top padding, right? -->

        <View android:layout_width="50dp"
              android:layout_height="50dp"
              android:background="@color/red" />

    </FrameLayout>

</FrameLayout>

推荐答案

我的问题是最有可能涉及到多个/嵌套 fitsSystemWindows 属性,这是我发现不工作后来。我通过应用属性有一种观点认为解决我的问题,然后垫复制到通过 ViewTreeObserver.OnGlobalLayoutListener 需要他们的其他意见。这是一个丑陋的黑客攻击,但它的工作现在。

My problem was most likely related to multiple/nested fitsSystemWindows attributes which does not work as I found out later. I solved my problem by applying the attribute to one view, and then copy the paddings to other views that need them via an ViewTreeObserver.OnGlobalLayoutListener. That is an ugly hack, but does its job for now.