如何像WhatsApp一样左右对齐Android聊天应用程序

时间:2019-05-06 12:19:03

标签: android firebase firebase-realtime-database

我正在关注youtube上的tuturiol,以创建聊天应用。我面临的问题是我似乎并没有像whatsapp那样显示聊天记录,例如发件人应该在右边,用户应该在右边。我尝试了几种使用if else语句似乎与一侧对齐的方法。这是我的代码。而且它不像其他问题,因为我有2种不同的xmls,而我正在尝试使用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);
}

我应该在哪里指定正确的对齐方式?

0 个答案:

没有答案
相关问题