如何使内容在非全宽工具栏后面绘制(如新的Gmail或Google App)

时间:2019-04-07 17:22:52

标签: android android-layout android-toolbar android-appbarlayout

我要实现的工具栏不是全角(在16dp的所有边都有空白),如下所示:

Gmail-请注意,可以在工具栏Gmail后面滚动看到recyclerview

Google App-同样,可以在工具栏后面看到卡片。 Google App

此外,这些工具栏在向下滚动时隐藏,在向上滚动时显示。

工具栏的内容不是我现在担心的。

我假设这是使用协调器布局完成的,所以这是我拥有的骨架:

  • 协调器布局
    • AppBarLayout
      • 工具栏
    • NestedScrollView(appbar_scrolling_view_behavior)
      • ConstraintLayout
<androidx.coordinatorlayout.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:animateLayoutChanges="true"
    tools:context=".MainContentFragment">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_margin="32dp"
        android:background="@android:color/transparent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/holo_red_dark"
            app:layout_scrollFlags="scroll|enterAlways">


        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        ...

这是滚动之前的输出 output

然后向下滚动 output2

您可以看到工具栏外部的空间不是透明的,而是具有灰色背景,因为内容在其下方并且无法看到。

0 个答案:

没有答案