【问题标题】:Adding a textview in a constraint layout inside a recycler view shorten the constraint layout width在回收站视图内的约束布局中添加文本视图会缩短约束布局宽度
【发布时间】:2020-06-09 16:33:57
【问题描述】:

我在约束布局(依赖项是 androidx.constraintlayout:constraintlayout:2.0.0-beta4)中有一个回收站视图(依赖项是 com.android.support:recyclerview-v7:28.0.0)和这个回收站的项目view 是另一种约束布局。

问题是:当我没有在项目布局中放置任何东西时,一切都很好,并且我的约束布局与回收器视图的宽度相匹配,但是当我尝试添加文本视图时,约束布局宽度缩短以匹配文本视图宽度

我已经为约束布局和文本视图(匹配、换行、0dp)尝试了不同的 layout_width 组合,但没有任何效果。

这是包含回收器视图的片段布局:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://[enter image description here][1]schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/champions_rv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="vertical"
        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"
        tools:listitem="@layout/item_composition"
        android:background="@android:color/holo_blue_light"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这是项目布局:

<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="wrap_content"
    android:background="@android:color/holo_orange_dark"
    android:paddingStart="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingEnd="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_purple"
        android:text="Text view"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

This is the result when the contraint layout is empty - it's good

And this is the result when I try to add the textview - This is not good

【问题讨论】:

  • 如果您在 RecyclerView 适配器 (onCreateViewHolder()) 中为 RecyclerView 项目充气,您是在指定父级还是指定 null?如果为 null,您需要为通货膨胀指定一个父级以知道它可以有多大。
  • 我的 onCreateViewHolder 方法使用扩展 ConstraintLayout 的简单自定义视图类返回视图。我删除了自定义视图并将其替换为经典的 LayoutInflater.inflate 行,现在一切都很好。 谢谢老兄

标签: android-layout android-recyclerview android-constraintlayout androidx constraintlayout-guideline


【解决方案1】:

TextView 的宽度应与其约束相匹配,而不是包装其内容。这将使TextView 扩展为填充ConstraintLayout

<TextView
    android:id="@+id/tv_name"
    android:layout_width="0dp"

【讨论】:

  • 感谢您的回答,但遗憾的是这不起作用。事实上,那是我第一个代码的一部分,而且渲染很奇怪(layout_width="0dp" : ibb.co/3T5VyhX
  • 你是对的,这更奇怪......嗯。我现在很困惑,但也许以后会发生一些事情。
猜你喜欢
  • 2018-05-22
  • 2019-05-10
  • 1970-01-01
  • 2017-10-09
  • 2018-08-30
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多