【发布时间】:2021-12-10 11:51:10
【问题描述】:
我正在使用具有垂直偏差的约束布局来填充回收器视图中的视图。我成功地做到了。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0" />
</androidx.constraintlayout.widget.ConstraintLayout>
我的 recyclerview 项目以相反的顺序显示我,因为我想制作聊天应用程序。所以我添加了 reverseLayout 和 stackFromEnd true。所以它看起来像这样。
如果我的项目是单一的,我的 ui 通过上面的 xml 代码转换成这样的
现在我想在底部添加编辑文本和按钮。它不工作的垂直偏差。有没有人知道如何解决。关于我可以使用什么的任何建议
预期输出
场景 1
场景 2
实际输出
我试过这段代码
<androidx.constraintlayout.widget.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"
android:background="@color/white">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart=10dp"
android:paddingEnd="10dp"
android:paddingBottom="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toTopOf="@+id/inputContainer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/inputContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/crecyclerView">
<EditText
android:id="@+id/editTextContainer"
android:layout_width="200dp"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/editTextContainer"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
更新
添加@MartinMarconcini 回答后,它修复了我的场景 2。我也想修复为场景1。
【问题讨论】:
-
设置_RecyclerView_高度为
0dp。 -
@Cheticamp 它不起作用。当项目更多时,倒序的第一个项目显示半切,如果项目在一个中,则保持相反的顺序。
-
@Cheticamp 你还有什么建议吗?
-
我所说的将 RecyclerView 保留在 EditText 和 Button 上方,无论 Button 中有多少项目i>回收站视图。我不能理解你对布局的问题,因为根据我的理解,这对我有用。
-
@Cheticamp 当我设置为 0dp 时它可以工作,但是我希望我的 recyclerview 填充整个高度并在图像 1 中显示预期的输出。
标签: android android-layout android-linearlayout android-relativelayout android-constraintlayout