问题出在 match_parent 和 wrap_content 上的约束布局

huangapple go评论80阅读模式
英文:

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:

match_parent_image

问题出在 match_parent 和 wrap_content 上的约束布局

wrap_content_image

问题出在 match_parent 和 wrap_content 上的约束布局

&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginTop=&quot;8dp&quot;
android:layout_marginEnd=&quot;8dp&quot;
android:layout_marginBottom=&quot;8dp&quot;
android:background=&quot;@drawable/message_sent_background&quot;&gt;

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

&lt;TextView
    android:id=&quot;@+id/textViewChatMessageDate&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_marginEnd=&quot;4dp&quot;
    app:layout_constraintBottom_toBottomOf=&quot;@+id/imageViewChatMessageStatus&quot;
    app:layout_constraintEnd_toStartOf=&quot;@+id/imageViewChatMessageStatus&quot;
    app:layout_constraintStart_toEndOf=&quot;@+id/textViewChatMessage&quot;
    app:layout_constraintTop_toTopOf=&quot;@+id/imageViewChatMessageStatus&quot;
    tools:text=&quot;12:02&quot; /&gt;

&lt;ImageView
    android:id=&quot;@+id/imageViewChatMessageStatus&quot;
    android:layout_width=&quot;18sp&quot;
    android:layout_height=&quot;18sp&quot;
    android:layout_marginTop=&quot;8dp&quot;
    android:layout_marginEnd=&quot;8dp&quot;
    android:layout_marginBottom=&quot;8dp&quot;
    app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
    app:layout_constraintEnd_toEndOf=&quot;parent&quot;
    app:layout_constraintTop_toTopOf=&quot;parent&quot;
    app:srcCompat=&quot;@drawable/ic_message_waiting&quot; /&gt;

&lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

答案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.

huangapple
  • 本文由 发表于 2020年9月21日 18:48:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63990764.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定