匹配父宽度属性在recyclerView中不起作用 [英] match parent width property not work in recyclerView

查看:139
本文介绍了匹配父宽度属性在recyclerView中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试将match_parent width属性设置为viewHolder的textView,但其工作原理与wrap_context相同.

I tried to set the match_parent width property to the textView of viewHolder, but it works the same as the wrap_context.

// recyclerview
<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/maintainer_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    app:listMaintainer="@{viewModel.maintainers}"/>

// viewHolder item
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/maintainer_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@color/color_black"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="maintainer_name" />

更新

感谢@MikeM.因为我在viewHolder中使用了数据绑定,并且没有正确地将其添加到 onCreateViewHolder 方法中的适配器中,所以会导致此问题.

Thanks to @Mike M. because I use data binding in viewHolder and not correctly add it to adapter within onCreateViewHolder method, so it cause this issue.

推荐答案

当使ConstraintLayout的子项的宽度为 match_parent 时,无需设置水平约束(开始和结束)).如果您从TextView中删除行 app:layout_constraintStart_toStartOf =" parent" ,它将呈现您想要的确切方式.

When you make a child of a ConstraintLayout have a width of match_parent, there's no need to set an horizontal constraint (start and end). If you remove the line app:layout_constraintStart_toStartOf="parent" from your TextView, it would render the exact way you want it to.

回顾一下,您可以通过两种方式完成自己想做的事情.我说明了以下两种方法:

To recap, there are two ways you can do what you want to do. I illustrated the two approaches below:

  1. 将TextView的宽度设置为 match_parent 并排除任何水平约束:

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

<TextView
    android:id="@+id/maintainer_name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:background="@color/color_black"
    android:textSize="24sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="maintainer_name" />

</androidx.constraintlayout.widget.ConstraintLayout>


  1. 将TextView的宽度设置为 0dp 并包括水平约束:

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

<TextView
    android:id="@+id/maintainer_name"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:background="@color/color_black"
    android:textSize="24sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="maintainer_name" />

</androidx.constraintlayout.widget.ConstraintLayout>

这篇关于匹配父宽度属性在recyclerView中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆