Tôi đang phát triển một ứng dụng nhắn tin tức thời cho một khóa học đại học, tương tự như Whatsapp như bạn có thể đoán thấy biểu tượng trong ảnh chụp màn hình đính kèm :).Sắp xếp đúng TextViews trong RelativeLayout
Tôi có một ListFragment được điền bằng CursorAdapter tùy chỉnh. Nó cung cấp cho hai loại quan điểm, gửi tin nhắn (liên kết đến bên phải) và nhận được tin nhắn (liên kết sang trái).
Bố cục cho tin nhắn đã gửi hoạt động tốt, nhưng tôi không thể nói giống với tin nhắn đã nhận.
Đây là một ảnh chụp màn hình:
bố trí nhắn Sent:
<?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>
bố trí nhắn đã nhận:
<?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>
Những màu nền xấu xí được thêm vào chỉ để xem khoảng trống cho mỗi chế độ xem và bố cục. Như bạn có thể thấy trong ảnh chụp màn hình, gửi tin nhắn (phải liên kết) hoạt động tốt. Các thư đã nhận (được căn trái) không được sắp xếp tốt, vì RelativeLayout mất tất cả không gian ngang sẵn có, được tạo thành từ nội dung gói.
Bất cứ ai cũng biết cách khắc phục hoặc có thể thiết kế bố cục tốt hơn để thực hiện những gì tôi cần?
Cảm ơn rất nhiều.
+1 cho một ảnh chụp màn hình mà nói với tất cả những câu chuyện ...:) – Farhan
những điều ngu ngốc và cà phê là tốt đẹp để tiếp tục khi có việc phải làm và thiếu thời gian :) – mavnaranjo
+1 cho ảnh chụp màn hình của bạn. – PhatHV