加载RecyclerView后RelativeLayout高度异常增大问题排查
解决RecyclerView填充数据后覆盖底部按钮的问题
你的问题出在RelativeLayout的layout_height设置错误。在垂直方向的LinearLayout中,使用layout_weight分配剩余空间时,子布局的高度必须设为0dp,而非match_parent。原来的设置会让RelativeLayout优先尝试占满屏幕高度,再加上权重计算的冲突,导致RecyclerView填充数据后,整个布局被内容撑大,进而覆盖底部按钮。
直接修改一处即可解决:
将RelativeLayout的android:layout_height从match_parent改为0dp,保留android:layout_weight="1"。这样LinearLayout会自动给它分配上下按钮之外的剩余高度,不会被内容拉伸;RecyclerView的android:layout_height="match_parent"可以保留,超出的内容会自动以滚动形式展示。
修改后的完整布局代码:
<?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat 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" android:padding="25dp"> <Button android:id="@+id/download_btn" android:layout_width="match_parent" android:layout_height="100dp" android:text="download all canadian universities" android:textColor="#FFFFFF" android:textSize="22sp" android:background="@drawable/buttonshape" android:shadowColor="#A8A8A8" android:layout_weight="0" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" <!-- 此处修改为0dp --> android:layout_weight="1" android:background="#FFAA22AA"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cleared!" android:visibility="gone"/> <android.support.v7.widget.RecyclerView android:id="@+id/rvSchools" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout> <Button android:id="@+id/clear_btn" android:layout_width="match_parent" android:layout_height="100dp" android:text="clear" android:textColor="#FFFFFF" android:textSize="22sp" android:background="@drawable/buttonshape" android:shadowColor="#A8A8A8" android:layout_weight="0" /> </androidx.appcompat.widget.LinearLayoutCompat>
修改后,RelativeLayout和RecyclerView的高度会固定在屏幕剩余空间,填充数据后只会内部滚动,不会覆盖底部按钮。
内容的提问来源于stack exchange,提问作者Drake
相关产品推荐
相关产品推荐

