我正在开发一款即时通讯应用程序,与Whatsapp类似,您可以通过附加的屏幕截图中的图标猜测出来。我使用自定义的CursorAdapter填充ListFragment,它提供两种类型的视图:已发送消息(靠右对齐)和接收到的消息(靠左对齐)。已发送消息的布局效果良好,但接收到的消息则不尽如人意。以下是屏幕截图:
已发送消息的布局:
那些丑陋的背景色只是为了看到每个视图和布局所占用的空间。如屏幕截图所示,发送消息(右对齐)效果很好。而接收到的消息(左对齐)没有对齐好,因为RelativeLayout占据了所有可用的水平空间,而不是只包装内容。 有人知道如何修复它,或者有更好的布局设计来完成我需要的功能吗?非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FFCCCCCC"
android:padding="5dp" >
<TextView
android:id="@+id/ceo_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FFBBBBBB"
android:gravity="center"
android:textSize="10sp" />
<TextView
android:id="@+id/ceo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/ceo_timestamp"
android:background="#FFAAAAAA"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
收到的消息布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#FF999999"
android:padding="5dp" >
<TextView
android:id="@+id/cei_timestamp"
android:layout_width="60sp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#FF888888"
android:gravity="center"
android:textSize="10sp" />
<TextView
android:id="@+id/cei_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/cei_timestamp"
android:background="#FF777777"
android:gravity="right"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
那些丑陋的背景色只是为了看到每个视图和布局所占用的空间。如屏幕截图所示,发送消息(右对齐)效果很好。而接收到的消息(左对齐)没有对齐好,因为RelativeLayout占据了所有可用的水平空间,而不是只包装内容。 有人知道如何修复它,或者有更好的布局设计来完成我需要的功能吗?非常感谢。