android listview 聊天,Android在ListView中实现聊天泡泡

本文讲述了作者在Android聊天应用中遇到的聊天泡泡对齐难题,重点在于如何让传入消息的文本根据长度自动调整聊天泡泡。通过MessageAdapter源码分析,展示了如何根据不同消息方向设置相对布局,并利用9-patch图像实现不同样式。
摘要由CSDN通过智能技术生成

我正在

Android上编写一个聊天客户端,但是在我的客户端中有一个聊天泡泡问题.我的聊天屏幕包括一个包含文本框的ListView和底部的发送按钮.对于外发消息,文本在ListView行中保持对齐.对于传入消息,文本在ListView行中对齐.但是,聊天泡泡不会调整到传入消息文本的长度.左对齐的传出消息不会发生此问题.

这里的屏幕截图如下.

聊天消息文本存储在数据库中,并通过光标适配器显示在ListView中.在MessageAdapter的Java源代码中,即时确定聊天文本的对齐方式.这两个聊天泡泡都是使用Android的9个补丁图像完成的.

下面是我的聊天活动布局,ListView的问题是messageHistoryList:

android:orientation="vertical"

android:padding="10dip"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

android:id="@+id/messageHistoryList"

android:layout_width="wrap_content"

android:layout_height="0px"

android:layout_weight="1"/>

android:layout_width="fill_parent"

android:layout_height="wrap_content" >

android:id="@+id/message"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:gravity="top"

android:layout_weight="1"/>

android:id="@+id/sendMessageButton"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="4"

android:text="Send"/>

ListView行布局:

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/userAndMessage">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textUser"

android:textStyle="bold"

android:textColor="@color/blue"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textMessage"

android:textColor="@color/blue"

android:textStyle="bold"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/textTime"

android:textColor="@color/blue"/>

留言适配器

public class MessageAdapter extends SimpleCursorAdapter {

static final String[] FROM = { ChatHistoryManager.C_TIME };

static final int[] TO = { R.id.textTime };

static final int MESSAGE_INCOMING_DIR = 1;

private String incomingMessageUserName;

private String selfUserName;

public MessageAdapter(Context context, Cursor cursor) {

super(context, R.layout.message_list_item, cursor, FROM, TO);

}

@Override

public void bindView(View row, Context context, Cursor cursor) {

super.bindView(row, context, cursor);

int messageDir = cursor.getInt(cursor.getColumnIndex(ChatHistoryManager.C_DIR));

if(messageDir == MESSAGE_INCOMING_DIR) {

RelativeLayout.LayoutParams userNameAndChatMessageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

userNameAndChatMessageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

RelativeLayout.LayoutParams userNameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

userNameParams.addRule(RelativeLayout.LEFT_OF, R.id.textMessage);

RelativeLayout.LayoutParams chatMessageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

chatMessageParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, R.id.textUser);

RelativeLayout.LayoutParams timeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

timeParams.addRule(RelativeLayout.ALIGN_RIGHT, R.id.userAndMessage);

timeParams.addRule(RelativeLayout.BELOW, R.id.userAndMessage);

row.setBackgroundResource(R.color.grey);

// Set the chat message

String chatMessage = cursor.getString(cursor.getColumnIndex(ChatHistoryManager.C_TEXT));

TextView textMessage = (TextView) row.findViewById(R.id.textMessage);

textMessage.setText(chatMessage.trim());

textMessage.setLayoutParams(chatMessageParams);

// Format the time stamp of the message

long timestamp = cursor.getLong(cursor.getColumnIndex(ChatHistoryManager.C_TIME));

TextView textTime = (TextView) row.findViewById(R.id.textTime);

String readableTimeStamp = (String) DateUtils.getRelativeTimeSpanString(timestamp);

textTime.setText(readableTimeStamp.trim());

textTime.setLayoutParams(timeParams);

// Format the message owner and the message

TextView textUser = (TextView) row.findViewById(R.id.textUser);

textUser.setText(incomingMessageUserName + ": ");

textUser.setLayoutParams(userNameParams);

row.setBackgroundResource(R.drawable.incoming_chat_bubble);

}

else {

RelativeLayout.LayoutParams userNameAndChatMessageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

userNameAndChatMessageParams.addRule(RelativeLayout.RIGHT_OF, R.id.userImage);

RelativeLayout.LayoutParams userImageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

userImageParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

RelativeLayout.LayoutParams userNameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

userNameParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.userAndMessage);

RelativeLayout.LayoutParams chatMessageParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

chatMessageParams.addRule(RelativeLayout.RIGHT_OF, R.id.textUser);

RelativeLayout.LayoutParams timeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

timeParams.addRule(RelativeLayout.ALIGN_LEFT, R.id.userAndMessage);

timeParams.addRule(RelativeLayout.BELOW, R.id.userAndMessage);

// Set the chat message

String chatMessage = cursor.getString(cursor.getColumnIndex(ChatHistoryManager.C_TEXT));

TextView textMessage = (TextView) row.findViewById(R.id.textMessage);

textMessage.setText(chatMessage);

textMessage.setLayoutParams(chatMessageParams);

// Format the time stamp of the message

long timestamp = cursor.getLong(cursor.getColumnIndex(ChatHistoryManager.C_TIME));

TextView textTime = (TextView) row.findViewById(R.id.textTime);

textTime.setText(DateUtils.getRelativeTimeSpanString(timestamp));

textTime.setLayoutParams(timeParams);

// Format the message owner and the message

TextView textUser = (TextView) row.findViewById(R.id.textUser);

textUser.setText(selfUserName + ": ");

textUser.setLayoutParams(userNameParams);

row.setBackgroundResource(R.drawable.outgoing_chat_bubble);

}

}

public void setIncomingMessageUserName(String inputUserName) {

this.incomingMessageUserName = inputUserName;

}

public void setSelfUserName(String inputUserName) {

this.selfUserName = inputUserName;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值