CollapsingToolbarLayout:意外的视图堆栈行为

时间:2015-10-03 14:34:45

标签: android android-layout android-support-library android-framelayout android-collapsingtoolbarlayout

CollapsingToolbarLayout(自定义FrameLayout)的子项似乎不符合FrameLayout的预期堆栈行为。我希望视图按照XML布局中提供的顺序相互叠加。

在下面的示例中,折叠CollapsingToolbarLayout时," layout_button_bar" LinearLayout滚动到" button_follow"和" button_like"纽扣。如果在上面提到的按钮之后定义LinearLayout,我希望LinearLayout滚动两个按钮,遮挡它们,而不是在它们下面。有人熟悉解决方法吗?

<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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/collapsible_app_bar_height"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@color/background_content_frame"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/some_image"
                app:layout_collapseMode="parallax"/>

            <ImageView
                android:id="@+id/image_header_gradient"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/scrim_top_bottom_banner"
                app:layout_collapseMode="parallax"
                tools:ignore="ContentDescription"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/collapsible_toolbar"
                android:layout_width="match_parent"
                android:layout_height="104dp"
                android:minHeight="?attr/actionBarSize"
                android:gravity="top"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:layout_collapseMode="pin"/>

            <Button
                android:id="@+id/button_follow"
                android:layout_width="@dimen/text_button_width"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|start"
                android:layout_marginStart="@dimen/bottom_landing_button_margin"
                android:layout_marginBottom="@dimen/slide_handle_height"
                android:gravity="center_vertical|start"
                android:drawablePadding="@dimen/spacing_xsmall"
                android:drawableStart="@drawable/selector_follow"
                android:visibility="gone"
                android:textColor="@color/vertigo_alabaster_white"
                app:layout_collapseMode="parallax"/>

            <Button
                android:id="@+id/button_like"
                android:layout_width="@dimen/text_button_width"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|start"
                android:layout_marginStart="@dimen/bottom_landing_button_margin"
                android:layout_marginBottom="@dimen/slide_handle_height"
                android:gravity="center_vertical|start"
                android:drawablePadding="@dimen/spacing_xsmall"
                android:drawableStart="@drawable/selector_like"
                android:visibility="gone"
                android:textColor="@color/vertigo_alabaster_white"
                app:layout_collapseMode="parallax"/>

            <LinearLayout
                android:id="@+id/layout_button_bar"
                android:layout_width="match_parent"
                android:layout_height="@dimen/button_bar_height"
                android:layout_gravity="bottom"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:background="@color/slide_handle">

                <!-- 3 Buttons -->

            </LinearLayout>

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

Library:com.android.support:design 23.0.1 |主题:Theme.AppCompat.NoActionBar

预塌陷

Before collapsing

中塌

Mid-collapsing

1 个答案:

答案 0 :(得分:0)

只需将ButtonButton包含在Layout中。例如LinearLayout

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:layout_marginBottom="80dp"
            app:layout_collapseMode="parallax">


            <Button
                android:id="@+id/button_follow"
                ...
                />

            <Button
                android:id="@+id/button_like"
                ...
                />

        </LinearLayout>