我正在创建一个聊天应用程序,并且一切准备就绪,问题是,当我在RecyclerView中显示文本时,friend聊天气泡会像用户发送的文本一样对齐。如何将其对准recyclerview的左侧?
我有2种不同的layot,每个气泡一个,以及一个recyclerview适配器。

这是一张解释我的问题的照片:

android - 将聊天气泡对准RecyclerView的左侧-LMLPHP

这是own_message.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:paddingEnd="15dp"
    android:paddingStart="60dp"
    android:clipToPadding="false">

    <TextView
        android:id="@+id/own_message_body"
        style="@style/ChatBubble"
        android:background="@drawable/own_message"
        tools:text="This is my own message, hope it is okay.">
    </TextView>

</RelativeLayout>


others_message.xml
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:layout_height="wrap_content"
    android:clipToPadding="false"
    android:paddingStart="15dp"
    android:paddingTop="10dp"
    android:paddingEnd="60dp"
    android:paddingBottom="10dp">


    <TextView
        android:id="@+id/others_message_body"
        style="@style/ChatBubble"
        android:background="@drawable/others_message"
        tools:text="Hey! I am a stranger, i'm chatting with you right now! :)" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignTop="@+id/others_message_body"
        android:layout_alignBottom="@+id/others_message_body"
        android:layout_marginStart="10dp"
        android:layout_toEndOf="@+id/others_message_body"
        android:contentDescription="@string/user_avatar"
        app:srcCompat="@drawable/ic_user_avatar">

    </ImageView>

</RelativeLayout>


而且,如果需要的话,这就是我所说的适配器的位置:
val layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
                binding.recyclerViewChat.layoutManager = layoutManager
                binding.recyclerViewChat.setHasFixedSize(true)
                val adapter = ChatAdapter(messages)
                binding.recyclerViewChat.adapter = adapter


如何将 friend 聊天气泡对准适配器的左侧?

最佳答案

您的两个布局都向右对齐。您希望others_message.xml左对齐。

您希望ImageViewstartparent对齐。然后,您希望TextView与该endImageView对齐。

因此,您需要执行以下操作:

  • android:layout_toEndOf="@+id/others_message_body"中的android:layout_alignParentStart="true"替换ImageView
  • ImageView一个id,例如android:id="@+id/imageViewId"
  • 通过添加TextView
  • ImageViewandroid:layout_alignEnd="@id/imageViewId"的末尾对齐
    others_message.xml的最终结果应如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <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="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false"
        android:paddingStart="15dp"
        android:paddingTop="10dp"
        android:paddingEnd="60dp"
        android:paddingBottom="10dp">
    
    
        <TextView
            android:id="@+id/others_message_body"
            style="@style/ChatBubble"
            android:background="@drawable/others_message"
            android:layout_alignEnd="@id/imageViewId"
            tools:text="Hey! I am a stranger, i'm chatting with you right now! :)" />
    
        <ImageView
            android:id="@+id/imageViewId"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignTop="@id/others_message_body"
            android:layout_alignBottom="@id/others_message_body"
            android:layout_marginStart="10dp"
            android:layout_alignParentStart="true"
            android:contentDescription="@string/user_avatar"
            app:srcCompat="@drawable/ic_user_avatar">
    
        </ImageView>
    
    </RelativeLayout>
    

    07-28 02:34
    查看更多