英文:
Problem with match_parent and wrap_content constraint layout
问题
<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>
英文:
I'm having trouble with ConstraintLayout in order to do the wrapped content into my layout. Every time I put wrap content the layout design gets like destroyed. Can somebody help me? It's about wrapping the content of the message before the date time and message status. Thanks!
Here I leave two images one with match_parent and the other with wrapped content:
<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
得分: 2
Wrap_content告诉你的元素要占用所需的空间,这可能导致图像占用它所需的空间,如果你希望你的元素保持在约束范围内,应该使用0dp,在约束布局中,0dp等同于match parent,0dp的作用是让你的元素填满约束,无论你的图像是比约束更大还是更小。
请阅读这个链接,它解释了约束布局的工作原理。
英文:
Wrap_content tells your element to take as much as space you need and that might result in the image taking the space it needs, If you want your element to stay in constraints you should use 0dp, in constraint layout 0dp is match parent, what 0dp does is tells you elements to fill up the constraint regardless if your image is bigger or smaller than the constraints.
Please give this a read, it explains how to constraint layout works.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论