match_parent 和 wrap_content 约束布局问题

Problem with match_parent and wrap_content constraint layout

我在使用 ConstraintLayout 时遇到问题,无法将包装的内容放入我的布局中。每次我放置包装内容时,布局设计都会被破坏。有人可以帮我吗?它是关于在日期时间和消息状态之前包装消息的内容。谢谢!

这里我留下两张图片,一张是 match_parent,另一张是包装内容:

match_parent_image

wrap_content_image

<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="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:background="@drawable/message_sent_background">

<TextView
    android:id="@+id/textViewChatMessage"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:textAppearance="?attr/textAppearanceListItem"
    android:textColor="@android:color/black"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textViewChatMessageDate"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Test message sent!" />

<TextView
    android:id="@+id/textViewChatMessageDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="4dp"
    app:layout_constraintBottom_toBottomOf="@+id/imageViewChatMessageStatus"
    app:layout_constraintEnd_toStartOf="@+id/imageViewChatMessageStatus"
    app:layout_constraintStart_toEndOf="@+id/textViewChatMessage"
    app:layout_constraintTop_toTopOf="@+id/imageViewChatMessageStatus"
    tools:text="12:02" />

<ImageView
    android:id="@+id/imageViewChatMessageStatus"
    android:layout_width="18sp"
    android:layout_height="18sp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@drawable/ic_message_waiting" />

</androidx.constraintlayout.widget.ConstraintLayout>

Wrap_content 告诉您的元素占用您需要的 space 数量,这可能会导致图像占用它需要的 space,如果您希望您的元素保留在constraints 你应该使用 0dp,在 constraint layout 0dp 是 match parent,0dp 的作用是告诉你元素填充约束,不管你的图像是大于还是小于约束。

请阅读this,它解释了约束布局的工作原理。