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

时间:2015-07-23 02:22:07

标签: android android-fragments android-design-library android-coordinatorlayout

使用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)

主要片段:

// Days late
System.out.print("Days late: ");
int daysLate = in.nextInt();

// You will probably need to get rid of the new line character here
in.nextLine();  

// Daily Fine
System.out.print("Daily fine: ");
double dailyFine = in.nextDouble();

// And here..
in.nextLine(); 

//daily fine calculation
double fine = daysLate * dailyFine;

第二个片段:

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

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

1 个答案:

答案 0 :(得分:7)

解决了它,感谢Chris Banes

问题是它不知道窗口插入。您必须在onViewCreated中请求ApplicationInsets。

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