Collapsing toolbar with image, tabs and FAB

2020-06-28 03:22发布

问题:

I'm having difficulties trying to implement the toolbar as in images below:

However, I've failed to add the Floating Action Button like shown on image. I'm using Googles design support library. Any ideas?

My current XML code:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

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

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="250dp"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.7">

                <ImageView
                    android:id="@+id/imgBackdrop"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#20000000" />

            </FrameLayout>


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                style="@style/Widget.PinList.ActionBar.Transparent"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                style="@style/PinListTabLayout"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                app:layout_collapseMode="pin" />

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/background"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

回答1:

I've found a solution. I thought that app:layout_anchorGravity could only reference elements like RelativeLayout's positioning attributes. But obviously, I was wrong. I've only added this code above the ViewPager in previous xml:

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="10dp"
    android:layout_marginRight="10dp"
    android:src="@drawable/ic_action_add"
    app:layout_anchor="@id/collapsing_toolbar"
    app:layout_anchorGravity="bottom|right|end" />


回答2:

See my solution here. It works on API 14-23. Hope to help you