首页 > 解决方案 > 如何创建类似 Netflix 的折叠透明工具栏

问题描述

如何像 netflix 一样创建折叠工具栏。recyclerview 可以在状态栏后面绘制,而工具栏则不能。

我尝试创建一个,但不知何故无法让它工作。将 设置android:fitsSystemWindows="false"为协调器布局也会影响工具栏。

示例图片

在此处输入图像描述

fragment_main.xml

<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:fitsSystemWindows="false"
    android:background="@color/netflix_black">
    <include

        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/content"
        layout="@layout/layout_content"/>
    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/toolBarLayoutView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp"
        android:background="@android:color/transparent"
        >
        <androidx.appcompat.widget.Toolbar

            android:id="@+id/toolbar"
            app:layout_scrollFlags="scroll|enterAlways"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">

            <ImageView
                android:layout_width="56dp"
                android:layout_height="match_parent"
                android:layout_marginStart="8dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:src ="@drawable/logo_netflix"
                />

        </androidx.appcompat.widget.Toolbar>

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

</androidx.coordinatorlayout.widget.CoordinatorLayout>

布局内容.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/toolBarLayoutView"
    android:layout_width="match_parent"
    android:background="@android:color/transparent"
    android:layout_height="match_parent"
   xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"

    >
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/parentRv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:overScrollMode="never"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

标签: androidandroid-layoutandroid-collapsingtoolbarlayout

解决方案


推荐阅读