Android布局调整求助:ScrollView内容被底部Button遮挡问题
嘿,我完全懂你现在的困扰——当ScrollView里的内容变长时,底部的Button会挡住部分内容对吧?这是因为当前布局里ScrollView的高度设为match_parent,它会占满AppBar之外的整个屏幕空间,而Button只是靠layout_gravity="bottom"浮在上面,两者的空间没有做合理划分,自然会出现遮挡。
给你两个可行的解决方案,你可以根据自己的实际需求选择:
方案一:用LinearLayout包裹ScrollView和Button(保留原有ScrollView结构)
我们可以把ScrollView和底部Button放在一个垂直方向的LinearLayout里,给ScrollView设置layout_weight="1",让它自动填充Button上方的所有剩余空间,这样无论内容多长,都会被限制在Button上方的区域里,再也不会被遮挡。
修改后的完整布局代码如下:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.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:background="@drawable/fundo_degrade" android:fitsSystemWindows="true" tools:context="br.com.cifrasemusica.cifrasmusica_teoriamusical.activity.ExercicioActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <include android:id="@+id/Toolbar_Exercicio" layout="@layout/toolbar" /> </android.support.design.widget.AppBarLayout> <!-- 用LinearLayout包裹ScrollView和Button,处理空间分配 --> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <android.support.v7.widget.RecyclerView android:id="@+id/RecyclerView_Exercicio" android:layout_width="match_parent" android:layout_height="wrap_content" /> </ScrollView> <Button android:id="@+id/Button_enviarExercicio" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/fundo_botao" android:text="CONFIRMAR" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
关键修改点:
- 新增垂直方向的LinearLayout,替代原来单独的ScrollView作为AppBar的滚动子视图,确保它能正确响应AppBar的滚动行为。
- ScrollView的高度从
match_parent改成0dp,并添加layout_weight="1",让它占据Button之外的所有可用空间。 - 移除Button的
layout_gravity="bottom"和layout_alignParentBottom="true",LinearLayout的垂直布局已经会把Button固定在底部。
方案二:移除ScrollView,直接用RecyclerView(更推荐)
其实RecyclerView本身就具备滚动功能,外面再套一层ScrollView会破坏它的视图复用机制,导致性能下降。如果你的页面核心内容就是这个RecyclerView,建议直接用RecyclerView和Button做空间分配:
<?xml version="1.0" encoding="utf-8"?> <android.support.design.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:background="@drawable/fundo_degrade" android:fitsSystemWindows="true" tools:context="br.com.cifrasemusica.cifrasmusica_teoriamusical.activity.ExercicioActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/ThemeOverlay.AppCompat.ActionBar"> <include android:id="@+id/Toolbar_Exercicio" layout="@layout/toolbar" /> </android.support.design.widget.AppBarLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior"> <android.support.v7.widget.RecyclerView android:id="@+id/RecyclerView_Exercicio" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> <Button android:id="@+id/Button_enviarExercicio" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" android:background="@drawable/fundo_botao" android:text="CONFIRMAR" /> </LinearLayout> </android.support.design.widget.CoordinatorLayout>
这样既解决了内容被遮挡的问题,又能保证RecyclerView的性能,一举两得~
内容的提问来源于stack exchange,提问作者DouglasNickson
相关产品推荐
相关产品推荐

