android:fitsSystemWindows 不工作

标签 android android-view

在我的主题中,我定义了以下规则来在状态栏后面绘制我的 View :

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

并在Activity中(onCreate):

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

然后,在我的 View 中,我使用了一个应该在状态栏后面绘制的 Barcodescanner,它正在工作。但是,当我将 android:fitsSystemWindows 应用于任何 subview 时,它们的位置不会得到调整。不过,当我将它应用于根元素时,它就起作用了。

<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>

Screenshot

最佳答案

使用 CoordinatorLayout 作为 View 的根,它对我有用。

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

关于android:fitsSystemWindows 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205473/

相关文章:

android - ScrollView 中 ConstraintLayout 的奇怪行为

android - 将 S3 Bucket 与 Dynamodb android 链接起来?

android - 如何撤消对窗口装饰 View 的更改?

android - 按钮自行松开(可能是特定于设备的)

android - attrs.xml 中用于自定义 View 的同名属性

Android AnimationDrawable 回收问题

android - Retrofit-http 405 响应?

java - 访问子节点的长度返回原始计数的两倍

android - 是否可以有一个透明的 SYSTEM_ALERT_WINDOW 让 TouchEvents 通过?

android - 将可运行对象发布到使该 View 无效的 View 有时不起作用