CollapsingToolbarLayout
是 Android 中用于实现可折叠工具栏的一个组件,它是 CoordinatorLayout
的子视图,通常与 AppBarLayout
和 Toolbar
结合使用。当 CollapsingToolbarLayout
折叠时显示工具栏图标,可以通过设置 CollapsingToolbarLayout
的属性来实现。
LinearLayout
,用于实现 Material Design 中的工具栏滑动效果。ActionBar
。以下是一个简单的示例,展示如何在 CollapsingToolbarLayout
折叠时显示工具栏图标:
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:src="@drawable/header_image"
app:layout_collapseMode="parallax"/>
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</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">
<!-- 这里放置你的内容视图 -->
</androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
问题: 当 CollapsingToolbarLayout
折叠时,工具栏图标不显示。
原因: 可能是因为 Toolbar
的 layout_collapseMode
属性没有设置为 pin
,导致工具栏在折叠时被隐藏。
解决方法: 确保 Toolbar
的 app:layout_collapseMode="pin"
属性已设置,这样工具栏在折叠时会固定在顶部显示。
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
通过以上设置,当 CollapsingToolbarLayout
折叠时,工具栏图标将会保持显示状态。
领取专属 10元无门槛券
手把手带您无忧上云