如何在CollapsingToolbarLayout中垂直排序视图?

时间:2018-01-18 07:59:15

标签: android android-coordinatorlayout android-collapsingtoolbarlayout

我无法理解CoordinatorLayout。我想在imageView下面放一个带有textView的工具栏。我希望两个视图都是CollapsingToolbarLayout的子项。我把工具栏放在imageView下面,但工具栏实际上出现在imageView的顶部。我试图运行带有和没有layout_anchor属性的代码,我得到相同的结果。我已经让工具栏显示在imageView下面,使其成为CoordinatorLayout的直接子项,但这不是最佳的,因为工具栏可能与我拥有的NestedScrollView重叠。任何帮助我理解这一点的帮助将不胜感激!

fragment_article_detail.xml

<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.design.widget.FloatingActionButton
        android:id="@+id/share_fab"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_marginRight="@dimen/fab_margin"
        android:background="@drawable/add_fab_background"
        android:backgroundTint="@color/theme_accent"
        android:backgroundTintMode="src_over"
        android:contentDescription="@string/action_share"
        android:elevation="@dimen/fab_elevation"
        android:src="@drawable/ic_share"
        android:stateListAnimator="@anim/fab_state_list_anim"
        app:layout_anchor="@id/title.toolbar"
        app:layout_anchorGravity="bottom|right|end" />

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:elevation="4dp">

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

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/title.toolbar"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_marginTop="24dp"
                android:background="@color/brand_primary"
                android:elevation="4dp"
                app:layout_anchor="@id/main.appbar"
                app:layout_anchorGravity="bottom|right"
                app:layout_collapseMode="pin"
                app:theme="@style/ThemeOverlay.AppCompat.Light">

                <LinearLayout
                    android:id="@+id/book_title_container"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginBottom="8dp"
                    android:minHeight="?android:attr/actionBarSize"
                    android:orientation="vertical">

                    <TextView
                        android:id="@+id/book.title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="40dp"
                        android:layout_marginRight="75dp"
                        android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse" />

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:id="@+id/book_body_container"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:transitionGroup="true">

            <TextView
                android:id="@+id/article_body"
                style="?android:attr/textAppearanceMedium"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="72dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="40dp"
                android:fontFamily="sans-serif"
                android:lineSpacingMultiplier="@fraction/detail_body_line_spacing_multiplier"
                android:padding="16dp"
                android:textSize="20sp" />
        </LinearLayout>

    </android.support.v4.widget.NestedScrollView>

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

1 个答案:

答案 0 :(得分:1)

注意: CollapsingToolbarLayout扩展Framelayout

您可以在重叠布局中将重力直接设置为工具栏,以将工具栏放在底部。

试试这个

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="md.com.collapsinglayoutx.MainActivity">

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

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


            <ImageView
                android:id="@+id/photo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:srcCompat="@drawable/canoe_water_nature"
                app:layout_collapseMode="parallax"/>


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                android:layout_gravity="bottom"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode="pin">

            <LinearLayout
                android:id="@+id/book_title_container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/book.title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Title"
                    android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
                    android:textColor="@color/colorAccent" />

            </LinearLayout>

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

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

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

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

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

<强>输出

enter image description here