如何在Toolbar与TabLayout间插入图片且不遮挡顶部工具栏
问题描述
想在AppBarLayout内的Toolbar和TabLayout之间插入一张图片(蓝色ImageView),实现TabLayout始终显示、Toolbar上滑时隐藏、图片仅在页面滚动到顶部时显示的效果,但目前出现图片始终遮挡顶部Toolbar的问题。
原布局代码
<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:orientation="vertical" tools:context=".Site.MainPage"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/mainPage_appBar" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/mainPage_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/UpperToolbarBackground" app:layout_scrollFlags="scroll|enterAlways"> <TextView android:id="@+id/mainPage_toolbar_title" style="@style/TextAppearance.AppCompat.Widget.ActionToolBar.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Test" android:textColor="@color/UpperToolbarText" /> </androidx.appcompat.widget.Toolbar> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll"> <ImageView android:id="@+id/mainPage_profile_picture" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/blue_700" app:layout_constraintDimensionRatio="1:1" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/mainPage_tabLayout" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_margin="0dp" app:layout_scrollFlags="noScroll" app:tabMode="auto" /> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:id="@+id/mainPage_nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior" app:layout_constraintTop_toBottomOf="@id/mainPage_appBar"> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/mainPage_viewPager" android:layout_width="match_parent" android:layout_height="0dp" android:nestedScrollingEnabled="true" /> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
当前实际效果
- 初始状态

- 向下滚动状态

- 少量上滚状态(问题出现位置)

预期实现效果

解决方案
问题根源
出现遮挡是因为两个原因:一是ScrollFlags配置缺少吸附逻辑,滑动过程中Toolbar和图片的滚动不同步;二是AppBarLayout内子View的Z轴层级默认是后面的View覆盖前面的,所以ImageView所在的布局会盖住Toolbar。
具体修改步骤
调整ScrollFlags配置
给Toolbar和图片所在的ConstraintLayout添加snap标记,保证滚动到一半时自动吸附到完全显示/隐藏状态,避免半滑状态的遮挡:- Toolbar的
app:layout_scrollFlags改为scroll|enterAlways|snap - 包裹ImageView的ConstraintLayout的
app:layout_scrollFlags改为scroll|snap
- Toolbar的
调整Z轴层级避免覆盖
给Toolbar设置更高的海拔,保证它始终在图片上层:- Toolbar添加属性
android:elevation="4dp" - 包裹ImageView的ConstraintLayout添加属性
android:elevation="2dp"
- Toolbar添加属性
优化滚动逻辑(可选,更贴合预期)
给AppBarLayout添加偏移监听,当Toolbar完全滑出屏幕后隐藏图片,避免滑动过程中出现冗余显示:mainPage_appBar.addOnOffsetChangedListener { appBarLayout, verticalOffset -> val toolbarHeight = mainPage_toolbar.height mainPage_profile_picture.visibility = if (Math.abs(verticalOffset) >= toolbarHeight) { View.GONE } else { View.VISIBLE } }修正冗余属性
删除NestedScrollView的app:layout_constraintTop_toBottomOf属性(父布局是CoordinatorLayout,该属性无效),同时将ViewPager2的高度改为match_parent。
修改后完整布局代码
<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" tools:context=".Site.MainPage"> <com.google.android.material.appbar.AppBarLayout android:id="@+id/mainPage_appBar" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/mainPage_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/UpperToolbarBackground" android:elevation="4dp" app:layout_scrollFlags="scroll|enterAlways|snap"> <TextView android:id="@+id/mainPage_toolbar_title" style="@style/TextAppearance.AppCompat.Widget.ActionToolBar.Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="Test" android:textColor="@color/UpperToolbarText" /> </androidx.appcompat.widget.Toolbar> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:elevation="2dp" app:layout_scrollFlags="scroll|snap"> <ImageView android:id="@+id/mainPage_profile_picture" android:layout_width="match_parent" android:layout_height="0dp" android:background="@color/blue_700" app:layout_constraintDimensionRatio="1:1" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout> <com.google.android.material.tabs.TabLayout android:id="@+id/mainPage_tabLayout" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_margin="0dp" app:layout_scrollFlags="noScroll" app:tabMode="auto" /> </com.google.android.material.appbar.AppBarLayout> <androidx.core.widget.NestedScrollView android:id="@+id/mainPage_nestedScrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <androidx.viewpager2.widget.ViewPager2 android:id="@+id/mainPage_viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:nestedScrollingEnabled="true" /> </androidx.core.widget.NestedScrollView> </androidx.coordinatorlayout.widget.CoordinatorLayout>
内容的提问来源于stack exchange,提问作者Steve
相关产品推荐
相关产品推荐

