Android Studio kotlin 聊天屏幕布局

标签 android android-studio kotlin

我正在尝试构建聊天布局。一切正常。当用户相互匹配时,他们可以写消息并且消息出现在聊天屏幕中。但是,我的聊天气泡之间存在巨大差距。

我阅读了官方 Android Chat Bubble 文档,浏览了 YouTube 视频和堆栈溢出。我真的不明白我哪里错了。我的气泡是实时出现的,我可以在使用两个虚拟 android 设备进行测试时读取用户的聊天记录,但是聊天记录之间存在巨大的差距,并且彼此之间没有重叠。

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.ChatActivity">


<LinearLayout
    android:id="@+id/navigationLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/navigation_height"
    android:layout_margin="@dimen/chat_margin"
    android:background="@color/shadow"
    android:orientation="horizontal"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <ImageView
        android:id="@+id/topPhotoIV"
        android:layout_width="@dimen/navigation_height"
        android:layout_height="@dimen/navigation_height"
        android:layout_marginRight="@dimen/chat_margin"
        android:scaleType="centerInside"/>

    <TextView
        android:id="@+id/topNameTV"
        android:layout_width="match_parent"
        android:layout_height="@dimen/navigation_height"
        android:gravity="center_vertical"
        android:textSize="20sp"
        android:paddingLeft="@dimen/chat_margin"/>

</LinearLayout>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/messagesRV"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    app:layout_constraintBottom_toTopOf="@+id/messageET"
    app:layout_constraintTop_toBottomOf="@+id/navigationLayout" />

<Button
    android:id="@+id/sendButton"
    style="@style/sendButton"
    android:layout_width="@dimen/send_width"
    android:layout_height="@dimen/send_height"
    android:layout_margin="@dimen/chat_margin"
    android:onClick="onSend"
    android:text="@string/send_button_text"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>

<EditText
    android:id="@+id/messageET"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:maxHeight="@dimen/send_height_max"
    android:layout_margin="@dimen/chat_margin"
    android:padding="@dimen/chat_margin"
    android:gravity="center_vertical"
    android:background="@drawable/chat_box_bg_rounded"
    android:hint="@string/hint_message"
    android:minHeight="@dimen/send_height"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/sendButton"
    app:layout_constraintStart_toStartOf="parent" />

chat screen

chatxml

currentUserMessage

otherUserMessage

我最终将回收站 View 更改为 wrap_content,它仍然存在差距。我想我在这里遗漏了什么?

StillAGap

所以我进入了 item_current_user_message.xml 和 item_other_user_message.xml 并将 match_parent 切换为 wrap_content 瞧,它起作用了。 :) 谢谢你!

FIXEDchatBubbles

最佳答案

这个问题是因为 match_parent 高度到聊天适配器的 layout。确保适配器的布局的高度应为 wrap_content。将高度设置为 wrap_content 将解决问题。

关于Android Studio kotlin 聊天屏幕布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59019424/

相关文章:

Android Studio 更新后的 Android Gradle CompilationFailedException

Android Studio,不匹配支持库版本错误,但不知道它在哪里指定

reflection - Kotlin:isAssignableFrom 和反射类型检查

kotlin - 类型与可空类型和可选类型不匹配

java - Android SQLite,如何将Cursor变成字符串?

android - Google I/O 2010 Android 应用程序是开源的吗?

java - Android 照片上的 EXIF 方向字段为空

android - 如果使用Android构建工具v0.6.1,则Android Studio在启动时显示错误

android - 在 safeArgs 中将 ArrayList 作为参数传递

android - 如何为选项卡 TabWidget 添加父对齐?