文章目录
- xml布局内容
- 预览画面(看着没毛病):
- 实际画面:
- 解决办法
- 说明
xml布局内容
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"><Buttonandroid:id="@+id/button_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/next"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/textview_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/hello_first_fragment"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@id/button_first" /><android.support.v7.widget.RecyclerViewandroid:id="@+id/rvList"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"app:layout_constraintTop_toBottomOf="@+id/textview_first" /></android.support.constraint.ConstraintLayout>
预览画面(看着没毛病):
实际画面:
- 列表数多超出顶部
- 100个item,从0开始,99结束,现只能看到97
解决办法
修改布局属性
得到期望的结果
说明
其实是犯了两个错误。
- 没有指定recycleview的底部约束,item数没超过显示区域时,是按照从顶往下排列的,当超过显示区域时候,依旧从上往下排列,但有一部分显示不出来
- 指定底部约束时候,高度wrap_content,当item数量没超过显示区域时,竖直方向是“居中”显示的,超过后底部仍然部分显示不出,且顶部还会超过一些。会遮挡其他view。
因此需要指定底部约束(app:layout_constraintBottom_toBottomOf=“parent”)的同时,还要设置高度为0dp(android:layout_height=“0dp”),基本完美解决超出遮挡、不从顶部排列、显示不全的问题。
此处android:layout_height=“0dp” 代表填充上下约束包围的区域
当然,还可以将ConstraintLayout换成其他ViewGroup也可以解决。
更多可以找关于ConstraintLayout的相关资料了解。