Android中RelativeLayout内ScrollView被底部按钮遮挡的解决方法
解决ScrollView底部内容被底部按钮遮挡的问题
这个问题太常见啦!你现在的问题根源在于ScrollView设置了android:layout_height="match_parent",它会占满整个屏幕高度,而底部的Button是叠加在ScrollView上面的,所以滚动到底部时最后一部分内容就被按钮挡住了。
只需要给ScrollView添加一个布局约束,让它的底部停在按钮的上方,就能完美解决这个问题:
修改后的RelativeLayout布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/add"> <!-- 关键:让ScrollView在按钮上方结束 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 你的大量列表内容 --> </LinearLayout> </ScrollView> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:paddingTop="23dp" android:text="@string/add" /> </RelativeLayout>
原理说明
添加android:layout_above="@+id/add"后,ScrollView会自动调整自身高度,使其底部刚好对齐Button的顶部,这样ScrollView的滚动区域就只会占据屏幕除去按钮高度的剩余空间,滚动到底部时内容就不会被按钮遮挡了。
另外还有一种替代方案,如果你愿意改用LinearLayout作为根布局,也可以这样实现:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <!-- 让ScrollView占据剩余所有空间 --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 你的大量列表内容 --> </LinearLayout> </ScrollView> <Button android:id="@+id/add" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="23dp" android:text="@string/add" /> </LinearLayout>
这种方式通过layout_weight让ScrollView自动填充按钮之外的空间,同样能达到效果,看你更习惯哪种布局方式啦~
内容的提问来源于stack exchange,提问作者Quillion
相关产品推荐
相关产品推荐

