Android ConstraintLayout-包装内容+;布局问题的解决

Android ConstraintLayout-包装内容+;布局问题的解决,android,android-constraintlayout,Android,Android Constraintlayout,我正在使用约束布局,我正在尝试将textview设置在指南的右侧,并环绕其宽度。但是,textview没有使用约束 “app:layout\u constraintLeft\u toRightOf=“@id/guideline1”出于某种原因 然而,这导致了一个问题,即短消息一直延伸到指南的左侧,从而产生了空白 我如何获得它,使消息布局位于指南的右侧,并包装其内容 这就是我想要的: 我使用了包裹宽度和水平偏差来实现它 <TextView android:id="@+id/

我正在使用约束布局,我正在尝试将textview设置在指南的右侧,并环绕其宽度。但是,textview没有使用约束
“app:layout\u constraintLeft\u toRightOf=“@id/guideline1”
出于某种原因

然而,这导致了一个问题,即短消息一直延伸到指南的左侧,从而产生了空白

我如何获得它,使消息布局位于指南的右侧,并包装其内容

这就是我想要的:

我使用了包裹宽度和水平偏差来实现它

<TextView
      android:id="@+id/textview_chatmodel_message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/bubble_user"
      android:padding="8dp"
      android:text="Username: aaaaaaaa "
      android:textColor="@color/colorBlack"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintLeft_toRightOf="@id/guideline1"
      app:layout_constraintHorizontal_bias="1"
      android:gravity="right"/>

但是这会导致不遵守
左侧指南的问题
约束:


如何获得它,使消息布局位于指南的右侧,并包装其内容?

一种解决方案可以是将文本视图包装在视图组中,然后包装视图组(例如,文本右对齐的相对布局)当前将具有与textView相同的约束,并且0dp宽度?…我知道它需要一个额外的视图,这有点令人不快。

如果视图设置为
wrap\u content
,当其大小超过可用空间时,它将忽略这些约束。为了避免这种情况,您必须添加此属性:

app:layout\u constrainedWidth=“true”
这是在ConstraintLayout版本1.1中添加的,在早期版本中,您需要将宽度设置为
match\u constraint
0dp
以产生类似的行为。


如果一个维度被设置为包装内容,在1.1之前的版本中,它们将被视为文字维度——这意味着,约束将不会限制结果维度,在某些情况下,您可能希望使用WRAP_内容,但继续强制约束以限制生成的维度。在这种情况下,您可以添加一个相应的属性:

app:layout_constrainedWidth=“true | false”

app:layout\u constrained height=“true | false”


这是一个可能的解决方案,但我想避免额外的看法哈哈。
android:layout_width="0dp"
<TextView
      android:id="@+id/textview_chatmodel_message"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@drawable/bubble_user"
      android:padding="8dp"
      android:text="Username: aaaaaaaa "
      android:textColor="@color/colorBlack"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintLeft_toRightOf="@id/guideline1"
      app:layout_constraintHorizontal_bias="1"
      android:gravity="right"/>