android – AppBarLayout滚动工具栏下方的内容

我想在我的工具栏下面有一个内容区域.它的行为应该是在向上滚动时滚动并在向下滚动时立即进入.工具栏应保持在其位置而不进行任何滚动.

我的布局是这样的:

<?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:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.MainActivity">

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

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

        <RelativeLayout
            android:id="@+id/box_search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            app:layout_scrollFlags="scroll|enterAlways">

            <TextView
                android:id="@+id/text_search_filter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Line 1"
                android:textColor="#FFF" />

            <TextView
                android:id="@+id/text_search_category"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text_search_filter"
                android:text="Line 2"
                android:textColor="#FFF" />

            <TextView
                android:id="@+id/text_search_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/text_search_category"
                android:text="Line 3"
                android:textColor="#FFF" />
        </RelativeLayout>
    </android.support.design.widget.AppBarLayout>

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

    <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"
        android:src="@drawable/ic_add_white_24dp" />
</android.support.design.widget.CoordinatorLayout>

如果我将内容区域放在工具栏上方,我可以获得所需的效果,但显然它处于错误的位置.

如果我把内容区域放在工具栏下面没有任何滚动…

最佳答案

If I put content area above Toolbar I am able to get the desired
effect, but obviously it’s in the wrong position.

当然,它自从:http://www.google.com/design/spec/layout/structure.html#structure-app-bar以来处于错误的位置

The app bar, formerly known as the action bar in Android, is a special
kind of toolbar
that’s used for branding, navigation, search, and
actions.

因此,您需要添加CollapsingToolbarLayout及其内容.

和:

I’d like to have a content area below my Toolbar. Its behavior should
be to scroll while scrolling up and to enter immediately while
scrolling down, Toolbar should stay on its place without any
scrolling.

要在添加CollapsingToolbarLayout后不滚动工具栏,您可能需要将app:layout_collapseMode =“pin”添加到该工具栏.

转载注明原文:android – AppBarLayout滚动工具栏下方的内容 - 代码日志