FrameLayout布局中ScrollView被BottomNavigationView遮挡问题解决方案咨询
问题根因
当前布局使用FrameLayout作为根容器,FrameLayout遵循层叠布局规则,子View按添加顺序先后绘制,后添加的BottomNavigationView固定在底部且层级更高,自然会遮挡住下层ScrollView的底部内容。
可行解决方案
方案1:替换根布局为垂直LinearLayout(最简便)
将外层FrameLayout替换为垂直方向的LinearLayout,让ScrollView占用底部导航之外的所有剩余空间,不会出现重叠:
<!-- 根布局换成垂直LinearLayout --> <LinearLayout 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:background="@color/colorBlack" android:id="@+id/main_container" android:orientation="vertical" tools:context=".MainActivity"> <!-- ScrollView设置weight=1占满剩余空间,高度设为0dp --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> .... </LinearLayout> </ScrollView> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/main_bnv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" app:itemIconTint="@drawable/menu_selector_color" app:itemTextColor="@drawable/menu_selector_color" app:labelVisibilityMode="unlabeled" app:menu="@menu/navi_menu"/> </LinearLayout>
方案2:保留FrameLayout,给ScrollView加底部内边距
如果业务需要必须保留FrameLayout作为根布局,可以给ScrollView增加底部内边距避开导航栏区域:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent" <!-- 底部padding等于导航栏高度,标准Material底部导航高度为56dp --> android:paddingBottom="56dp" <!-- 关闭padding裁剪,滑动时内容可以展示到padding区域 --> android:clipToPadding="false"> ... </ScrollView>
方案3:替换根布局为ConstraintLayout(灵活适配性最强)
用ConstraintLayout作为根布局,通过约束关系明确指定ScrollView的底部边界在导航栏上方:
<androidx.constraintlayout.widget.ConstraintLayout 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:background="@color/colorBlack" android:id="@+id/main_container" tools:context=".MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/main_bnv" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> .... </LinearLayout> </ScrollView> <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/main_bnv" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#000000" app:layout_constraintBottom_toBottomOf="parent" app:itemIconTint="@drawable/menu_selector_color" app:itemTextColor="@drawable/menu_selector_color" app:labelVisibilityMode="unlabeled" app:menu="@menu/navi_menu"/> </androidx.constraintlayout.widget.ConstraintLayout>
注意事项
不要给ScrollView设置android:layout_height="wrap_content",该属性会让ScrollView的高度等于内部子控件总高度,失去滚动控件的特性,推荐使用match_parent或者0dp配合权重/约束使用。
内容的提问来源于stack exchange,提问作者ho ya
相关产品推荐
相关产品推荐

