聊天活动的约束布局

问题描述 投票:2回答:3

我正在尝试使用约束布局在RecyclerView中设计聊天项,但是无法正确设置灵活宽度。

Android约束布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/receive_message_wrapper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


    <TextView
            android:id="@+id/receive_message_label"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAlignment="textStart"
            android:textColor="@color/receive_message_box_text"
            android:background="@drawable/message_recieve"
            android:padding="@dimen/message_box_padding"

            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toStartOf="@id/receive_time_label"

            app:layout_constraintHorizontal_chainStyle="spread_inside"
    />


    <TextView
            android:id="@+id/receive_time_label"
            android:layout_width="wrap_content"
            app:layout_constrainedWidth="true"
            android:layout_height="wrap_content"
            android:text="timestamp"
            android:textAlignment="textEnd"

            android:layout_marginStart="@dimen/space_between_message_element"

            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@id/receive_message_label"
            app:layout_constraintEnd_toEndOf="parent"

            tools:ignore="HardcodedText"
    />

</android.support.constraint.ConstraintLayout>

它运行良好,但如果我显示一些更长的消息,时间戳将被接收消息标签隐藏。

device behaviour layout in designer

android android-recyclerview android-constraintlayout
3个回答
1
投票

来自anber的解决方案很好,但是聊天内容textView将具有扩展的宽度,即使内容非常短,也可能变得不好。

我也做了一些非常相似的事情。最后我使用了一个解决方案:

  • 使用width = 0(match_constraint)的LinearLayout(或任何其他布局)来替换TextView
  • 将TextView作为上述布局的子项移动,并设置其width = wrap_content

因此,时间将显示,聊天内容宽度仍然包装。


0
投票

在你的时间TextView删除此行

app:layout_constraintStart_toEndOf="@id/receive_message_label"

因此,时间标签不仅限于消息,而只限于消息。

将消息标签宽度也设置为0dp

android:layout_width="0dp"

0
投票

UPD。我以前的解决方案不正确,因为@ aimin-pan提到了。

你应该为你设置消息textView android:layout_width="wrap_content"app:layout_constrainedWidth="true",以及在textView的左侧之前的右边约束。而时间textView只是在父级的右上方对齐。

enter image description here

在这种情况下,如果您有短文本,它将是换行上下文:enter image description here

如果文本很长,它也会纠正换行:enter image description here

完整的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:background="#F00"
        android:text="asdf asdf aasdf"
        app:layout_constrainedWidth="true"
        app:layout_constraintEnd_toStartOf="@+id/button"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
© www.soinside.com 2019 - 2024. All rights reserved.