android - ConstraintLayout 内的 RecyclerView - 父级顶部的起始位置
问题描述
为简单起见,我有 aConstraintLayout
和 2 个孩子: aRecyclerView
和 a Button
。
我希望RecyclerView
从父级顶部开始显示。如果只有几个项目要显示RecyclerView
,它应该将它们包装起来。像这样的东西:
但是,如果RecyclerView
有足够的项目直到屏幕末尾,它应该改为直到 的顶部,而Button
不是直到其父级的底部。像这样:
为了达到这个效果,我尝试了以下组合:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
RecyclerView
当只有几个项目要显示时,这个解决方案非常好。否则,如果它必须扩展到屏幕之外,它将落后于Button
.
将上述 xml 修改为(注意 处的行app:layout_constraintBottom_toTopOf="@id/my_button"
)后RecyclerView
:
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@id/my_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
我得到的结果是:
也就是说,它RecyclerView
位于父级顶部和按钮顶部之间的中心。如果有很多项目,这将是可以的RecyclerView
,但如果只有少量项目,则不行。
问题:是否有可能让我的RecyclerView
行为像:
不管项目的数量如何,使其顶部定位在其父项的顶部。
如果项目很少,只需将内容包装到最后一项的底部即可。
如果项目很多,展开到顶部,而
Button
不是底部parent
?
PS 该解决方案应考虑ConstraintLayout
仅用作父级。
解决方案
您可以使用app:layout_constraintHorizontal_bias
值为 的属性0
来对齐您RecyclerView
的顶部约束(1
将对齐底部约束)。需要设置顶部和底部约束以使用此偏差。另一方面,如果未设置底部约束,那么您将无法防止RecyclerView
与按钮重叠。这就是为什么设置底部约束并保留app:layout_constrainedHeight="true"
以确保View's
在其高度设置为 时强制执行约束很重要的原因wrap_content
。
您的顶部约束RecyclerView
也是不正确的,因为它的顶部应该包含在父级的顶部而不是底部。
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_bias="0.0"
app:layout_constraintBottom_toTopOf="@id/my_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/my_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
推荐阅读
- android - 如何创建一个在单击时打开对话框的 EditText 框?
- python - AttributeError:不能在指定中间模型的 ManyToManyField 上使用 remove()。改用songs.Playlist_Activity的管理器
- javascript - Three.js - 在 PlaneGeometry 上放置动态 2D 画布
- html - 溢出:隐藏在很长的单元格中不能在 colgroups 中工作
- flutter - FutureBuilder 返回传递给 var Flutter
- html - 标题没有移动到中心和响应式设计问题
- google-apps-script - 接收错误 参数 (number[]) 与 SpreadsheetApp.Range.setValues 的方法签名不匹配
- python - 一次配置我的 Tkinter 应用程序的所有按钮的理想方式?
- oracle - 创建存储过程时如何在 select into 子句中使用 case 语句
- agora.io - 新的 Web NG SDK(与 Vue.js 一起使用)