ConstraintLayout 设置约束优先级?

Luc*_* P. 5 android android-constraintlayout

我正在尝试使用ConstraintLayout.

我的布局

但是,这两个用户名TextViews需要扩展直到达到最大宽度,这取决于每个手机的屏幕。

第一个用户名应该是可见的,尽可能多,它应该有一个 8dp 的边距,然后是具有固定宽度的“回复箭头”,然后,如果合适,尽可能多的“回复用户名”TextView应该是可见的. 点分隔符、日期和喜欢的TexViews应该是可见的,在wrap_content.

在 iOS 中,我们已经通过使用内容拥抱和内容压缩阻力优先级来实现这样的布局。Android 中是否有类似的功能?

这是我的 XML 布局供参考:

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/newspaperLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

<View
    android:id="@+id/commentDepthView"
    android:layout_width="2dp"
    android:layout_height="0dp"
    android:layout_marginStart="@dimen/default_margin"
    android:background="@color/blueDark"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

<TextView
    android:id="@+id/commentAuthorTextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginTop="@dimen/default_margin"
    android:ellipsize="end"
    android:lines="1"
    android:textColor="@color/blue"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@id/commentTextView"
    app:layout_constraintEnd_toStartOf="@id/replyToArrowImageView"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@+id/commentDepthView"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Username"/>

<ImageView
    android:id="@+id/replyToArrowImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginEnd="8dp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@id/commentAuthorTextView"
    app:layout_constraintEnd_toStartOf="@id/commentReplyToTextView"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/commentAuthorTextView"
    app:layout_constraintTop_toTopOf="@id/commentAuthorTextView"
    app:srcCompat="@drawable/reply_to_arrow"/>

<TextView
    android:id="@+id/commentReplyToTextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:ellipsize="end"
    android:lines="1"
    android:textColor="@color/blue"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@+id/commentAuthorTextView"
    app:layout_constraintEnd_toStartOf="@+id/dotSeparatorTextView"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintHorizontal_chainStyle="packed"
    app:layout_constraintStart_toEndOf="@id/replyToArrowImageView"
    app:layout_constraintTop_toTopOf="@id/commentAuthorTextView"
    tools:text="Reply To Username"
    />

<TextView
    android:id="@+id/dotSeparatorTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginEnd="8dp"
    android:text="@string/dot_separator"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="@id/commentAuthorTextView"
    app:layout_constraintEnd_toStartOf="@+id/commentDateTextView"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/commentReplyToTextView"
    app:layout_constraintTop_toTopOf="@id/commentAuthorTextView"/>

<TextView
    android:id="@+id/commentDateTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginEnd="@dimen/default_margin"
    app:layout_constraintBottom_toBottomOf="@+id/commentAuthorTextView"
    app:layout_constraintEnd_toStartOf="@id/commentLikesTextView"
    app:layout_constraintStart_toEndOf="@id/dotSeparatorTextView"
    app:layout_constraintTop_toTopOf="@+id/commentAuthorTextView"
    tools:text="1/1/1970"/>

<TextView
    android:id="@+id/commentLikesTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginEnd="@dimen/default_margin"
    app:layout_constraintBottom_toBottomOf="@+id/commentAuthorTextView"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/commentDateTextView"
    app:layout_constraintTop_toTopOf="@+id/commentAuthorTextView"
    tools:text="0 Likes"/>


<TextView
    android:id="@+id/commentTextView"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginTop="@dimen/default_margin"
    android:layout_marginEnd="@dimen/default_margin"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/commentDepthView"
    app:layout_constraintTop_toBottomOf="@+id/commentAuthorTextView"
    tools:text="This is test comment that is really relly long. Well not that long, but it's long enough, don't you think?"/>

<View
    android:id="@+id/separatorView"
    android:layout_width="0dp"
    android:layout_height="0.5dp"
    android:layout_marginStart="@dimen/default_margin"
    android:layout_marginTop="@dimen/default_margin"
    android:layout_marginEnd="@dimen/default_margin"
    android:background="@color/grey"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toEndOf="@id/commentDepthView"
    app:layout_constraintTop_toBottomOf="@+id/commentTextView"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)

欢迎任何建议,即使使用与ConstraintLayout. 非常感谢。

小智 5

给出第一个用户名

android:layout_width="0dp" 
app:layout_constraintHorizontal_bias="0"
app:layout_constraintWidth_default="wrap"
Run Code Online (Sandbox Code Playgroud)

和第二个 android:layout_width="0dp"

其余的是 wrap_content 并将它们链接起来