CoordinatorLayout 状态栏填充在片段事务期间消失

CoordinatorLayout status bar padding disappears during fragment transactions

使用 22.2.1 设计支持库和 API 22(尚未在早期版本上测试),我 运行 在片段之间切换时遇到状态栏填充问题。初始片段加载正常,但在片段事务之后,状态栏填充消失,将所有视图推到它们不应该出现的位置。弹出返回堆栈后,原始片段也会发生同样的事情。旋转设备可以修复它,打开软键盘也是如此(但仅限纵向,不能横向)。

main fragment on initial load or after rotation(desired)

main fragment after back pressed

other fragment when loaded

other fragment after rotation or keyboard(desired)

主要片段:

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

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    style="@style/RecyclerView"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_person_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp"/>

第二个片段:

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

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/card_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/collapsingToolbar_height"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="@dimen/default_margin"
        app:expandedTitleMarginEnd="@dimen/sheet_expanded_title_margin"
        app:expandedTitleMarginStart="@dimen/sheet_expanded_title_margin"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.CollapsingToolbarLayout>

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="@dimen/default_tab_layout_height"
        app:tabGravity="center"
        app:tabMinWidth="120dp"
        app:tabMode="scrollable"/>

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    style="@style/Fab"
    android:src="@drawable/ic_add_white_24dp"
    app:backgroundTint="@color/accent_dark"
    app:borderWidth="2dp" />

主题将 windowDrawsSystemBarBackgrounds 设置为 true,并将 statusBarColor 设置为透明。

解决了,感谢Chris Banes

问题是它不知道 window 插入。您必须在 onViewCreated 中请求 ApplyInsets。

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ViewCompat.requestApplyInsets(coordinatorLayout);
}