Android 将聊天气泡与RecyclerView的左侧对齐

Android 将聊天气泡与RecyclerView的左侧对齐,android,xml,kotlin,android-recyclerview,Android,Xml,Kotlin,Android Recyclerview,我正在创建一个聊天应用程序,我已经准备好了一切,问题是,当我在RecyclerView中显示我的文本时,friend聊天泡泡就像用户发送的文本一样对齐。如何将其与recyclerview的左侧对齐? 我有两个不同的布局,每个气泡一个,一个recyclerview适配器 这是一张解释我的问题的照片: 下面是own_message.xml: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="

我正在创建一个聊天应用程序,我已经准备好了一切,问题是,当我在RecyclerView中显示我的文本时,
friend
聊天泡泡就像用户发送的文本一样对齐。如何将其与recyclerview的左侧对齐? 我有两个不同的布局,每个气泡一个,一个recyclerview适配器

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

下面是
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>

如果需要,我可以在这里调用适配器:

val layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
                binding.recyclerViewChat.layoutManager = layoutManager
                binding.recyclerViewChat.setHasFixedSize(true)
                val adapter = ChatAdapter(messages)
                binding.recyclerViewChat.adapter = adapter


如何将好友聊天泡泡与适配器的左侧对齐?

您的两个布局都与右侧对齐。您希望
others\u message.xml
向左对齐

您希望您的
ImageView
parent
start
对齐。然后,您希望
TextView
ImageView
端对齐

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

  • 图像视图中将
    android:layout\u toEndOf=“@+id/others\u message\u body”
    替换为
    android:layout\u alignParentStart=“true”
  • ImageView
    一个
    id
    ,例如
    android:id=“@+id/imageViewId”
  • 通过添加
    android:layout\u alignEnd=“@id/imageViewId”
  • others\u message.xml
    的最终结果应该如下所示:


    两个布局都向右对齐。您希望
    others\u message.xml
    向左对齐

    您希望您的
    ImageView
    parent
    start
    对齐。然后,您希望
    TextView
    ImageView
    端对齐

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

  • 图像视图中将
    android:layout\u toEndOf=“@+id/others\u message\u body”
    替换为
    android:layout\u alignParentStart=“true”
  • ImageView
    一个
    id
    ,例如
    android:id=“@+id/imageViewId”
  • 通过添加
    android:layout\u alignEnd=“@id/imageViewId”
  • others\u message.xml
    的最终结果应该如下所示:

    
    
    你能分享你的
    @style/chatbuble
    吗。此外,我认为问题在于
    ImageView
    TextView
    对于
    others\u message.xml
    。布局应与起点对齐。因此,将
    android:layout\u toEndOf=“@+id/others\u message\u body”
    替换为
    android:layout\u alignParentStart=“true”
    。然后,给
    ImageView
    一个
    id
    ,比如
    imageViewId
    ,然后添加到TextView
    android:layout\u alignEnd=“@id/imageViewId”
    ,你明白了!你愿意回答这个问题吗?好的,先生,请给我一点时间:D你能分享一下你的
    @style/chatbuble
    。此外,我认为问题在于
    ImageView
    TextView
    对于
    others\u message.xml
    。布局应与起点对齐。因此,将
    android:layout\u toEndOf=“@+id/others\u message\u body”
    替换为
    android:layout\u alignParentStart=“true”
    。然后,给
    ImageView
    一个
    id
    ,比如
    imageViewId
    ,然后添加到TextView
    android:layout\u alignEnd=“@id/imageViewId”
    ,你明白了!你愿意回答这个问题吗?好的,先生,请稍等。D
    val layoutManager = LinearLayoutManager(applicationContext, RecyclerView.VERTICAL, false)
                    binding.recyclerViewChat.layoutManager = layoutManager
                    binding.recyclerViewChat.setHasFixedSize(true)
                    val adapter = ChatAdapter(messages)
                    binding.recyclerViewChat.adapter = adapter