我无法通过ConstraintLayout使我的RecyclerView适应,该ConstraintLayout在AlertDialog中包含一些TextView。当RecyclerView适应屏幕大小时,没有问题:
但是,当有更多项目时,RecyclerView会在顶部和底部流动:
我通过在RecyclerView滚动条上将底部项目的可见性设置为false来容忍底部流程。但是,我不希望它流到顶部。我尝试将RecyclerView的高度设置为match_parent和0dp,这会导致更严重的问题。如何防止流量?
这是我的布局代码:
<?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:background="@color/light_gray"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/createBetslipRecycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/gray_900"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/bottomHolder"
/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottomHolder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
>
<TextView
android:id="@+id/totalOddsText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/TotalOdd"
android:textColor="@color/gray_800"
android:background="@color/transparent"
android:textSize="15sp"
android:textStyle="bold"
android:layout_marginTop="3dp"
app:layout_constraintTop_toTopOf="@id/amountEntry"
app:layout_constraintBottom_toBottomOf="@id/amountEntry"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp" />
<TextView
android:id="@+id/colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textColor="@color/gray_800"
android:background="@color/transparent"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="@+id/totalOddsText"
app:layout_constraintTop_toTopOf="@+id/totalOddsText"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
/>
<TextView
android:id="@+id/totalOddsValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12.80"
android:textColor="@color/gray_800"
android:background="@color/transparent"
android:textSize="15sp"
app:layout_constraintStart_toEndOf="@+id/colon"
app:layout_constraintTop_toTopOf="@+id/totalOddsText"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
/>
<EditText
android:id="@+id/amountEntry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="8"
android:hint="@string/EnterAmount"
android:inputType="numberDecimal"
android:textColor="@color/gray_800"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintEnd_toEndOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
确保ConstraintLayout的每个直接子级在水平和垂直方向上都受到约束。例如,createBetslipRecycler
不受水平限制。要解决此问题,请使width = 0dp
并在左侧和右侧约束父级。
以类似的方式将约束固定在其他视图上。 ConstraintLayout的子元素的宽度或高度不应为match_parent
。从documentation:
重要:不建议对ConstraintLayout中包含的小部件使用MATCH_PARENT。可以通过使用MATCH_CONSTRAINT来定义类似的行为,并将相应的左/右或上/下约束设置为“父”。
这应该是皮棉规则,但不是。
清除约束后,将RecyclerView和bottomHolder
放置在vertical packed chain中,该链接将在可用空间中链接RecyclerView和底部ConstraintLayout。
如果不能解决问题,请考虑使用上述正确的布局来更新帖子。