gpt4 book ai didi

Android sticky-footer : Align footer view to table, 直到达到屏幕大小,然后固定在底部

转载 作者:行者123 更新时间:2023-11-29 22:54:13 27 4
gpt4 key购买 nike

这应该类似于 iOS tableview 页脚,也可以在各种网站中看到(粘性页脚)。

我想实现以下目标:

diagram

A 是具有可变行数的 RecyclerView

A 小于屏幕(或父级)尺寸时,B(页脚)应放在最后一行的下方。

A + B大于屏幕尺寸时,B固定在底部,A内容是可滚动的。

我们目前正在使用 onMeasure 函数执行此操作,该函数计算所有组件的高度,以便相应地调整 A 的大小。

我想知道是否有更简单的方法,也许是 ConstraintLayout

最佳答案

AB 放入垂直打包链中,偏置为 0 使其与顶部对齐。您还需要为 RecyclerView 设置 app:layout_constrainedHeight="true" ,以便在它变得太大而不适合它们时考虑其约束(父级的高度保持 match_parent 在这种情况下):

<?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">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/A"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintVertical_chainStyle="packed"
app:layout_constraintVertical_bias="0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/B" />

<TextView
android:id="@+id/B"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Footer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/A"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

上述解决方案似乎不适用于 Constraintlayout:2.0.0-beta2,看起来像是该版本中引入的错误。适用于 2.0.0-beta11.1.3

另一种方法是将父项的高度设置为 wrap_content,然后您可以使用默认的 chainstyle 并消除偏差:

<?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="wrap_content">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/A"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/B" />

<TextView
android:id="@+id/B"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Footer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/A"
app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

此解决方案适用于所有版本。

关于Android sticky-footer : Align footer view to table, 直到达到屏幕大小,然后固定在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596891/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com