通过场景转换维护 "fitsSystemWindows" window 插图

Maintaining "fitsSystemWindows" window insets through Scene Transition

我有一个在 2 个场景之间转换的布局。

layout.xml

<FrameLayout
    android:id="@+id/framelayout_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    // Some background views.

    <FrameLayout
        android:id="@+id/scene_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <include layout="@layout/scene_a" />
    </FrameLayout>


</FrameLayout>

res/layouts/scene_a.xml

<android.support.constraint.ConstraintLayout
    android:id="@+id/constraintlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    // Child views removed for brevity
</android.support.constraint.ConstraintLayout>

场景B布局相同

在 activity 中,调用 ViewCompat.setOnApplyWindowInsetsListener(frameLayout, (v, insets) -> insets); 以使用 fitsSystemWindows with FrameLayout。

视图在初始视图上是正确的 inflation 即视图绘制在状态栏下方,但内容被 window 插入物向下推以避免状态栏。

但是,当我转换到场景 B 时,fitsSystemWindows 提供的填充丢失了,内容跳了起来。返回时,场景 A 也丢失了填充。

非常感谢有关如何在过渡期间保持此填充的任何帮助。

好的,所以与场景转换无关,而与 android:fitsSystemWindows 有关。

任何使用 android:fitsSystemWindows 的视图的默认行为是使用它传递的 window 插图。插图首先向下传递深度 - 请参阅此 blog

所以在我上面的例子中,第一个场景 - 设置了 android:fitsSystemWindows 消耗了插图,所以场景 B 没有机会。同样,当我转换回场景 A 时(它们已经被消耗)。

我的示例中的修复是从两个场景中删除 android:fitsSystemWindows 并将其放在场景根目录中。