如何制作 "wrap_content" 高度的折叠工具栏?

How to make CollapsingToolbar with "wrap_content" height?

我想让搜索工具栏在向下滑动时出现,在向上滑动时折叠,无论滚动级别如何。当我想将此工具栏的高度设置为与搜索区域完全相同时,我遇到了一个问题。向下滑动就好了。但向上滑动搜索图标仍然可见。 例如,如果我将 AppBarLayout android:layout_height="wrap_content" 更改为 100dp,则可以隐藏工具栏,但它看起来很糟糕并且可能会导致不同分辨率的问题。我想要实现的示例是在 Play Market 应用程序中进行搜索,它是如何完成的?

<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"
tools:context="test.proekt101_test.MainActivity">

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:layout_height="wrap_content">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways|snap"
        >
                    <SearchView
                    android:id="@+id/searchView1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="30dp"
                    >
                </SearchView>
                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>

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

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

也尝试将 android:fitsSystemWindows="true" 添加到 SearchView

android:fitsSystemWindows="true" is supposed to be on CoordinatorLayout, AppBarLayout, CollapsingToolbarLayout and the ImageView inside it.

来自 。为我工作。

android:fitsSystemWindows=“true”

对此负责。我找到了一篇有用的文章 — https://medium.com/google-developers/why-would-i-want-to-fitssystemwindows-4e26d9ce1eec#.psylrqm0y