首页 > 解决方案 > 如何在whatsapp中左右对齐android聊天应用程序

问题描述

我在 youtube 上关注一个 tuuriol 来创建一个聊天应用程序。我面临的问题是我似乎没有像whatsapp那样显示聊天,就像发件人应该在右边,而用户应该在右边。我尝试了几种方法,使用 if else 语句似乎与一侧对齐。这是我的代码。它与其他问题不同,因为我有 2 个不同的 xml,并且我正在尝试使用 java

chat_item_left.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<com.github.library.bubbleview.BubbleTextView
    android:layout_alignParentStart="true"
    android:layout_marginTop="5dp"
    android:layout_below="@id/messageUser"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1"
    android:textColor="#FFF"
    android:textSize="18sp"
    android:id="@+id/messageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="10dp"
    app:arrowWidth="8dp"
    app:angle="8dp"
    app:arrowHeight="10dp"
    app:arrowPosition="14dp"
    app:arrowLocation="left"
    app:bubbleColor="#333639"
    app:arrowCenter="true"
    android:text="hie"
    android:layout_alignParentLeft="true" />

chat_item_right.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

   <RelativeLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">

       <com.github.library.bubbleview.BubbleTextView
           android:layout_alignParentEnd="true"
           android:layout_marginTop="5dp"
           android:textAppearance="@style/TextAppearance.AppCompat.Body1"
           android:textColor="#FFF"
           android:textSize="18sp"
           android:id="@+id/messageText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:padding="10dp"
           app:arrowWidth="8dp"
           app:angle="8dp"
           app:arrowHeight="10dp"
           app:arrowPosition="14dp"
           app:arrowLocation="right"
           app:bubbleColor="@android:color/holo_blue_bright"
           app:arrowCenter="true"
           android:text="hie"
           android:layout_alignParentRight="true" />

这是我到目前为止对我的代码所做的事情

 private void displayChatMessage() {
    ListView listOfMessage = (ListView) findViewById(R.id.listOfMessage);
    Query query = FirebaseDatabase.getInstance().getReference().child("chats");

    FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
            .setLayout(R.layout.chat_item_left)
            .setQuery(query, ChatMessage.class)
            .build();

    adapter = new FirebaseListAdapter<ChatMessage>(options) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            // Bind the Chat to the view
            // ...
            //get references to views of list_item.xml
            TextView messageText ,messageUser,messageTime;
            messageText = (BubbleTextView) v.findViewById(R.id.messageText);


            messageText.setText(model.getMessageText());

        }
    };
    listOfMessage.setAdapter(adapter);
}

我在哪里指定放置正确的对齐?

标签: androidfirebasefirebase-realtime-database

解决方案


推荐阅读