首页 > 解决方案 > Create a auto fill chat message view in android

问题描述

I'm trying to design a chat message view like the one below: Whatsapp design

Everything is correct as long as the text is one-line. My design

But when the text is multi-linear, it looks like this: My design

How can I design a chat message view that automatically fills the space above the time-textView? Is it possible to do this only on XML?

My code:

<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:background="@drawable/nip_send_chat_bubble">


<ImageView
    android:id="@+id/seenStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="2dp"
    android:layout_marginLeft="2dp"
    android:layout_marginEnd="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginBottom="8dp"
    android:tint="?attr/metaTextColor"
    android:src="@drawable/message_sent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/time" />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="4dp"
    android:maxWidth="200dp"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore"
    android:textColor="?attr/textColor"
    android:textSize="16sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/time"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginLeft="4dp"
    android:layout_marginEnd="2dp"
    android:layout_marginRight="2dp"
    android:text="11:50 PM"
    android:textColor="?attr/metaTextColor"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/seenStatus"
    app:layout_constraintEnd_toStartOf="@+id/seenStatus"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/text"
    app:layout_constraintTop_toTopOf="@+id/seenStatus" />
</androidx.constraintlayout.widget.ConstraintLayout>

标签: androidandroid-layoutandroid-xml

解决方案


You could probably try to change the layout from Constraint to Relative Layout. Check if this piece of code helps you out.

<RelativeLayout 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:background="@drawable/nip_send_chat_bubble">


<ImageView
    android:id="@+id/seenStatus"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/text"
    android:tint="?attr/metaTextColor"
    android:src="@drawable/message_sent"
    android:layout_alignParentRight="true"
    />

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore"
    android:textColor="#000"
    android:textSize="16sp"
    />

<TextView
    android:id="@+id/time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="11:50 PM"
    android:textSize="12sp"
    android:layout_below="@id/text"
    android:layout_toLeftOf="@id/seenStatus"
    />

PS: I did not do the formatting so you can add that according to your needs.


推荐阅读