使用CollapsingToolbarLayout时如何在AppBarLayout和滚动内容之间插入LinearLayout

时间:2015-11-27 02:35:35

标签: android android-collapsingtoolbarlayout android-appbarlayout

我正在使用CollapsingToolbarLayout的CoordinatorLayout。我试图将一个LinearLayout放在AppBarLayout下面和滚动内容之上,我希望这个LinearLayout始终固定在屏幕上(隐藏或不隐藏AppBarLayout),而不是滚动内容。

这可能吗?

到目前为止,我有以下代码:

<android.support.design.widget.CoordinatorLayout
    android:layout_above="@+id/linearLayout"
    tools:context="..."
    android:layout_width="wrap_content"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="@dimen/app_bar_height"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay"
        android:minHeight="200dp"
        >

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

            <ImageView
                android:id="@+id/imagemView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"

                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                >

                <ImageButton
                    android:id="@+id/back_button"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@android:color/transparent"
                    android:src="@drawable/back_button"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingLeft="18dp"
                    android:textSize="25sp" />

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom|left|end"
        android:layout_margin="@dimen/fab_margin"
        app:borderWidth="0dp"
        app:layout_behavior="myBehavior"/>

    <include
        layout="@layout/content_scrolling" />

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

2 个答案:

答案 0 :(得分:3)

到目前为止,我通过以下方式得到了这个:

<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
        </CollapsingToolbarLayout>
    </AppBarLayout>

    <LinearLayout
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout/>//Layout which will stay between CollapsingToolbarLayout and Recyler or nested scroll view

    <Recycler Or Nested Scroll View/>

</CoordinatorLayout>

此处线性布局将滚动,但用户始终可以看到它。

答案 1 :(得分:0)

我认为你可以用这个实现这个目标

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:minHeight="200dp"
    >

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

        <ImageView
            android:id="@+id/imagemView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"

            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay"
            >

            <ImageButton
                android:id="@+id/back_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:src="@drawable/back_button"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="18dp"
                android:textSize="25sp" />

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

    </android.support.design.widget.CollapsingToolbarLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"/>
</android.support.design.widget.AppBarLayout>

然后包括

<include
    layout="@layout/content_scrolling"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

无需为LinearLayout设置任何滚动标记,因此CoordinatorLayout将仅隐藏CollapsingToolbarLayout