视图未占据全宽 - ConstraintLayout - RecyclerView - Android

View not taking full width - ConstraintLayout - RecyclerView - Android

我对 RecyclerView 中的项目有一个简单的布局,其中包含一个 TextView 和一个 RadioButton。这是布局xml

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tv_thali_name"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:gravity="center_vertical"
        android:hint="Name"       
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/materialRadioButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.radiobutton.MaterialRadioButton
        android:id="@+id/materialRadioButton"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

现在当我 运行 应用程序时,RecyclerView 看起来像这样 -

如果我为 TextView 设置固定宽度(我不想这样做),那么它会显示 TextView 内容。

我还在单独的布局中测试了 xml 代码,

如您所见,它正在显示应有的视图。但是当它作为 RecyclerView 中的项目使用时,只有 TextView 不显示。

我是不是漏掉了什么?

试试这个编辑:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.textview.MaterialTextView
    android:id="@+id/tv_thali_name"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:hint="Name"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.radiobutton.MaterialRadioButton
    android:id="@+id/materialRadioButton"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

我删除了一些额外的约束 app:layout_constraintEnd_toStartOf="@+id/materialRadioButton" 并添加了一些小的更改。这开始工作了。

好的,我修好了。我所要做的就是将 RecyclerView 宽度设置为 match_parent。以前是 0dp。