android - 聊天 Activity 的约束布局

标签 android android-recyclerview android-constraintlayout

我正在尝试在具有约束布局的 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

最佳答案

anber的解决方案很好,但是聊天内容textView的宽度会扩展,即使内容很短,这可能会变得不好。

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

  • 使用 width=0 (match_constraint) 的 LinearLayout(或任何其他布局)来替换 TextView

  • 将 TextView 移动为上述布局的子级,并设置其 width=wrap_content

因此时间无论如何都会显示,并且聊天内容宽度仍然换行。

关于android - 聊天 Activity 的约束布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55510905/

相关文章:

android - 如何将 ConstraintLayout 的子项放置在屏幕外(以便稍后可以将它们翻译到屏幕上)?

android - 从 Android 设备身份验证扫描仪获取指纹 JPEG 图像

Android:应用程序最终在执行后的异步任务中崩溃

android - 如何将消息从 Android 发送到 Unity(使用 VR)? (UnityPlayer.UnitySendMessage 不起作用)

android - 将 List<Model> 传递给嵌套适配器时为 null

java - Java 中约束布局宽度和高度作为根父级

android - 解析推送通知 android 设备 token 在某些情况下未保存

android - RecyclerView 适配器中的 onClickListener?

android - 合并多个 RecyclerView.Adapter 以用于单个 RecyclerView (Android)

android - ConstraintLayout 源代码在哪里?