如何在 CoordinatorLayout 中将固定视图显示到工具栏

How to show pinned view to Toolbar in CoordinatorLayout

如何在 AppBarLayout 或 Toolbar 上显示视图详细信息 Material 设计实现如下:

您需要使用 CoordinatorLayout 作为您的父 ViewGroup 并将您的详细视图锚定到您的工具栏,在我的例子中它是 CollapsingToolbarLayout。

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/annonce.main.coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="RtlHardcoded"
    android:text="hello"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/flexible.example.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="hello"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/flexible.example.collapsing"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            app:expandedTitleMarginBottom="94dp"
            android:text="hello"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleTextAppearance="@android:style/TextAppearance.Inverse"
            app:contentScrim="?colorPrimary">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:src="@mipmap/ic_launcher"
                android:tint="#BB3E50B4"
                android:scaleType="centerCrop"
                />

            <android.support.v7.widget.Toolbar
                android:id="@+id/flexible.example.toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@null"
                android:title="hello"
                app:layout_collapseMode="pin"
                />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="250dp"
        android:background="@android:color/holo_green_light"
        app:layout_anchor="@id/flexible.example.collapsing"
        app:layout_anchorGravity="bottom|center_horizontal"
        tools:ignore="RtlHardcoded"
        ></LinearLayout>
</android.support.design.widget.CoordinatorLayout>

这是我的布局预览:

最有趣的部分在LinearLayout:

        app:layout_anchor="@id/flexible.example.collapsing"
        app:layout_anchorGravity="bottom|center_horizontal"

其中描述了您的详细信息布局应固定的位置。

希望对您有所帮助