透明应用栏布局工具栏并将内容放在它们后面



到目前为止,我已经设法实现了带有滚动标志和应用栏滚动行为的 AppBarLayout 和工具栏,一切都很好用。我的最终目标是实现这样的东西:

网飞应用示例

如您所见,应用栏是透明的,内容在其后面。其他一切都完美无缺。

根据我放置 app:layout_behavior="@string/appbar_scrolling_view_behavior" 时的观察(在我的情况下,我使用了 app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"( 标记 CoordinatorLayout 不允许我重叠视图(在这种情况下,应用栏应该与我的内容重叠(。

这是我的 XML:

<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawerLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">
<fragment
android:id="@+id/fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="@navigation/root_nav_graph"
app:defaultNavHost="true"
/>
</FrameLayout>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$Behavior"
>
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"                  
app:layout_scrollFlags="scroll|enterAlways|snap"
/>
</com.google.android.material.appbar.AppBarLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

该片段包含将嵌套滚动视图作为根的布局。

删除属性

app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"

从框架布局。此外,将属性android:background="@android:color/transparent"设置为工具栏。

最新更新