什么应用程序组聊天视图 - Android

时间:2016-11-11 10:54:33

标签: android android-view android-xml android-framelayout

我需要实施群组聊天视图,例如什么应用。

请在下面找到我尝试过的示例代码

<LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:paddingBottom="@dimen/size_05"
            android:paddingTop="@dimen/size_05"
            android:layout_marginRight="@dimen/size_80"
            android:background="@drawable/chat_white_border"
            android:orientation="vertical">

            <TextView
                android:id="@+id/txt_friends_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:duplicateParentState="false"
                android:paddingLeft="@dimen/size_10"
                android:paddingRight="@dimen/size_10"
                android:text="name"
                android:textColor="@color/chat_friends_name"
                android:textSize="@dimen/size_12" />

            <view
                android:id="@+id/ll_c_friends_chat_message"
                class="com.view.ChatBubbleLayout"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/txt_friends_message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="left"
                    android:paddingLeft="@dimen/size_10"
                    android:paddingRight="@dimen/size_10"
                    android:text="name1" />

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|center|right"
                    android:orientation="horizontal">

                    <TextView
                        android:id="@+id/txt_rec_chat_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:paddingRight="@dimen/size_10"
                        android:text="11.00 PM"
                        android:textColor="@color/grey"
                        android:textSize="@dimen/size_10" />
                </LinearLayout>
            </view>
        </LinearLayout>
    </LinearLayout>

自定义视图文件:

package com.view;

import android.annotation.TargetApi;
import android.content.Context;
import android.text.Layout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
  * Created by Priyan
*/

public class ChatBubbleLayout extends FrameLayout {

public ChatBubbleLayout(Context context) {
    super(context);
}

public ChatBubbleLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ChatBubbleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
}

@TargetApi(21)
public ChatBubbleLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    TextView message_textView = (TextView) getChildAt(0);
    View localView = getChildAt(1);

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int view_size = View.MeasureSpec.getSize(widthMeasureSpec);
    Layout messageTextviewLayout = message_textView.getLayout();

    int linestart = messageTextviewLayout.getLineStart(messageTextviewLayout.getLineCount() - 1);
    int lineend = messageTextviewLayout.getLineEnd(messageTextviewLayout.getLineCount() - 1);

    int desiredWidth = (int) Layout.getDesiredWidth(message_textView.getText()
            .subSequence(linestart, lineend), message_textView.getPaint());

    int measuredWidth = message_textView.getMeasuredWidth();

    int requiredWidth = Math.min(measuredWidth,
            (int) Math.ceil(Layout.getDesiredWidth(message_textView.getText(),
                    message_textView.getPaint())) + message_textView.getPaddingRight() +
                    message_textView.getPaddingLeft());

    if (view_size - getPaddingLeft() - getPaddingRight()
            >= requiredWidth + localView.getMeasuredWidth()) {
        setMeasuredDimension(requiredWidth + localView.getMeasuredWidth() +
                getPaddingLeft() + getPaddingRight(), getMeasuredHeight());
    } else if ((requiredWidth - message_textView.getPaddingLeft() - message_textView.getPaddingRight()
            < desiredWidth + localView.getMeasuredWidth())) {
        setMeasuredDimension(getMeasuredWidth(),
                getMeasuredHeight() + localView.getMeasuredHeight());
    }
}
}

我的结果如下: enter image description here

您可以看到时间未正确对齐接收消息的视图的右侧...

我需要群聊聊天视图类似于什么应用...

0 个答案:

没有答案