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" />

聊天屏幕

聊天XML

当前用户消息

其他用户留言

我最终将回收者视图更改为wrap_content,它仍然存在差距。我想我在这里错过了什么?

斯蒂尔阿加普

所以我进入item_current_user_message.xml和item_other_user_message.xml,match_parent切换到wrap_content,瞧,它有效。 :)谢谢!

固定聊天气泡

此问题是由于聊天适配器的layoutmatch_parent。确保适配器的布局的高度应为wrap_content。将高度设置为wrap_content将解决此问题。

最新更新