match_parent和wrap_content约束布局的问题

时间:2020-09-21 10:48:33

标签: java android android-layout android-recyclerview android-constraintlayout

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

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

match_parent_image

enter image description here

wrap_content_image

enter image description here

<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>

1 个答案:

答案 0 :(得分:1)

Wrap_content告诉您的元素占用所需的空间,这可能会导致图像占用所需的空间。如果您希望元素保留在约束中,则应使用0dp,在约束布局中0dp是匹配父对象, 0dp的作用是告诉您元素填充约束,无论您的图像大于还是小于约束。

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