Android中如何根据消息发送方将布局分别显示在左右两侧?
搞定聊天消息左右布局的小技巧
你已经通过fromMe()判断了消息发送方,这一步做的很对!接下来要实现左右对齐显示,其实很简单,我给你两个实用的方案,选一个适合你的就行:
方案一:直接在布局文件里搞定(推荐)
这种方式最省心,分别给你的两个布局文件设置不同的对齐属性:
我的消息(靠右显示)mymessage.xml
用外层布局的gravity属性把消息气泡推到右边:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="end" <!-- 关键:让内部内容靠右 --> android:padding="8dp"> <!-- 这里是你的消息气泡容器,比如带圆角的LinearLayout --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/chat_bubble_mine" android:padding="12dp"> <TextView android:id="@+id/mymessageTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我发送的消息"/> <TextView android:id="@+id/mytimeTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="14:30" android:layout_marginLeft="8dp"/> </LinearLayout> </LinearLayout>
对方消息(靠左显示)message.xml
同样的结构,把gravity改成start(兼容RTL布局,比left更通用):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="start" <!-- 关键:让内部内容靠左 --> android:padding="8dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/chat_bubble_other" android:padding="12dp"> <TextView android:id="@+id/messageTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="对方发送的消息"/> <TextView android:id="@+id/timeTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="14:31" android:layout_marginLeft="8dp"/> </LinearLayout> </LinearLayout>
方案二:代码里动态调整布局参数
如果不想改布局文件,也可以在getView里动态设置对齐:
不过先提醒你一个小问题:你之前inflate对方布局时用了inflater.inflate(R.layout.message , parent, true),第三个参数传true会直接把布局添加到parent里,ListView自己会处理添加操作,所以这里应该改成false,避免重复添加的bug。
修改后的getView代码:
@Override public View getView(int position, View convertView, ViewGroup parent) { View messageView = null; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); if (messages.get(position).fromMe()) { messageView = inflater.inflate(R.layout.mymessage, parent, false); // 初始化控件 TextView timeView = messageView.findViewById(R.id.mytimeTextView); timeView.setText(messages.get(position).getTime()); TextView msgView = messageView.findViewById(R.id.mymessageTextView); msgView.setText(messages.get(position).getMessage()); // 设置靠右对齐 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.END; messageView.setLayoutParams(params); } else { messageView = inflater.inflate(R.layout.message, parent, false); // 这里改成false // 初始化控件 TextView timeView = messageView.findViewById(R.id.timeTextView); timeView.setText(messages.get(position).getTime()); TextView msgView = messageView.findViewById(R.id.messageTextView); msgView.setText(messages.get(position).getMessage()); // 设置靠左对齐 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); params.gravity = Gravity.START; messageView.setLayoutParams(params); } return messageView; }
额外小建议
- 最好用
ViewHolder模式优化一下,避免每次getView都重复findViewById,提升ListView的滑动流畅度; - 给消息气泡加个圆角背景,聊天界面会好看很多哦~
内容的提问来源于stack exchange,提问作者kane kayp
相关产品推荐
相关产品推荐

