简体   繁体   English

视图不全宽 - ConstraintLayout - RecyclerView - Android

[英]View not taking full width - ConstraintLayout - RecyclerView - Android

I have a simple layout for the items in RecyclerView, which contains a TextView and a RadioButton.我对 RecyclerView 中的项目有一个简单的布局,其中包含一个 TextView 和一个 RadioButton。 Here is the layout xml这是布局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>

Now when I run the app, the RecyclerView looks like this -现在,当我运行应用程序时,RecyclerView 看起来像这样 -

在此处输入图片说明

If I set a fixed width (which I don't want to) for the TextView, then it is showing the TextView content.如果我为 TextView 设置了固定宽度(我不想这样做),那么它会显示 TextView 内容。

I also tested the xml code in a separate layout,我还在单独的布局中测试了 xml 代码,

在此处输入图片说明

As you can see it is showing the view as it should.如您所见,它显示了它应该显示的视图。 But when using it as an item in RecyclerView, then only the TextView is not showing.但是当将它用作 RecyclerView 中的一个项目时,只有 TextView 没有显示。

Am I missing something?我错过了什么吗?

Try this edit:试试这个编辑:

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

I removed some extra constraint app:layout_constraintEnd_toStartOf="@+id/materialRadioButton" and added minor changes.我删除了一些额外的约束app:layout_constraintEnd_toStartOf="@+id/materialRadioButton"并添加了一些小的更改。 This got to work.这得起作用了。

Okay I fixed it.好的,我修好了。 All I had to do is setting the RecyclerView width as match_parent.我所要做的就是将 RecyclerView 宽度设置为 match_parent。 Previously it was 0dp.以前它是0dp。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM