Android可折叠布局无法正常工作

时间:2016-06-03 17:34:23

标签: android android-coordinatorlayout android-collapsingtoolbarlayout

我正在使用可折叠布局。这是主要的布局,

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout android:id="@+id/main_content"
                                                 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=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/bgheader"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            android:background="@drawable/scenary"
            app:layout_collapseMode="parallax" />


        <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
                                           xmlns:attrs="http://schemas.android.com/apk/res-auto"
                                           xmlns:app="http://schemas.android.com/apk/res-auto"
                                           android:id="@+id/toolbar"
                                           android:layout_width="match_parent"
                                           android:layout_height="?attr/actionBarSize"
                                           android:background="?attr/colorPrimary"
                                           app:layout_scrollFlags="scroll|enterAlways"
                                           app:popupTheme="@style/AppTheme.PopupOverlay">

            <FrameLayout
                android:id="@+id/titleContainer"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"

                android:gravity="center">
                <.utils.CustomFontTextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:text="Toolbar Title"
                    android:textColor="#ffffff"
                    attrs:customFont="handyman_bold"
                    android:textSize="8pt"
                    android:layout_marginLeft="-20dp"
                    android:id="@+id/toolbar_title"/>
            </FrameLayout>

            <ImageButton
                android:id="@+id/btn_ToolBarRightBtn"
                android:layout_width="32dp"
                android:layout_height="28dp"
                android:tag="0"
                android:background="@null"
                android:scaleType="fitXY"
                android:src="@drawable/icon_shopping"
                android:layout_alignParentRight="true"
                android:layout_gravity="right"
                android:layout_marginRight="10dp"
                />



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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_gravity="bottom"
        android:background="@color/offwhite"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

在ViewPager中我有一个回收站视图。这是我加载应用程序时看到的内容,

enter image description here

当我向上滚动回收者视图时,我看到了这一点,

enter image description here

我不确定为什么我最初会看到这个差距,之后它会充满应用原色。我在这做什么?

附上我的屏幕动画GIF。

enter image description here

由于

2 个答案:

答案 0 :(得分:1)

一个简单的解决方案是使用设计支持库版本23.1.1。此问题发生在设计支持库23.2.0以后。

compile 'com.android.support:design:23.1.1'

第二个解决方案是从android:fitsSystemWindows="true"删除行CoordinatorLayout

更新:

  

在这两种情况下,工具栏都与图像重叠,因此状态栏下的部分不可见

这种情况正在发生,因为CollapsingToolbarLayout类是FrameLayout的孩子。因此,您在framelayout中传递的视图将相互堆叠。这就是工具栏与图像重叠的原因。我找到的最简单的解决方案是将android:layout_marginTop="?attr/actionBarSize"放在ImageView

答案 1 :(得分:0)

FrameLayout中没有app:layout_behavior="@string/appbar_scrolling_view_behavior"

<FrameLayout
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:id="@+id/titleContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"