【问题标题】:CoordinatorLayout status bar padding disappears during fragment transactionsCoordinatorLayout 状态栏填充在片段事务期间消失
【发布时间】:2015-07-23 02:22:06
【问题描述】:

使用 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 为透明。

【问题讨论】:

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


    【解决方案1】:

    已解决,感谢Chris Banes

    问题是它不知道窗口插图。您必须在 onViewCreated 中请求应用插图。

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

    【讨论】:

    • 这将无法在 viewpager 中工作,因为可能只会显示第一个片段,但其余的则不会。
    • 它? “它”是什么意思?
    猜你喜欢
    • 2015-09-30
    • 1970-01-01
    • 2016-03-02
    • 2017-06-17
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    • 2019-08-04
    相关资源
    最近更新 更多