Vì vậy, tôi có hai TextViews
mỗi hàng trong một ListView
. Một nên được trái và bên phải khác liên kết. Cả hai TextViews
có một hình chữ nhật tròn làm nền mà chỉ cần bọc văn bản bên trong. Vì vậy, tôi đã đưa ra với điều này:Căn chỉnh hai TextViews, một bên trái sang phải, trên ListView mà không kéo dài nền
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="@drawable/bubble_purple"
android:gravity="center" >
</TextView>
<TextView
android:id="@+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@id/text_right"
android:background="@drawable/bubble_blue"
android:gravity="center" >
</TextView>
</RelativeLayout>
Nó có vẻ tốt cho văn bản dài nhưng không phải cho các thông điệp ngắn hơn:
Tôi cũng đã cố gắng một LinearLayout như thế này:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/text_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/bubble_blue"
android:gravity="center" >
</TextView>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:id="@+id/text_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/bubble_purple"
android:gravity="center">
</TextView>
</LinearLayout>
Tính năng này hoạt động đối với các tin nhắn ngắn nhưng không hoạt động đối với các tin nhắn ngắn hơn:
Bằng cách nào đó có thể đo chiều rộng kết hợp của TextViews
và chuyển đổi theo chương trình giữa các bố cục này hoặc tôi đang làm điều gì sai ở đây?
Đặt một số bố trí cân –
Hãy thử android: layout_weight = "1" cho cả hai – Android2390
Làm thế nào nên hai 'TextViews' căng nếu là người đầu tiên là nhỏ và cái thứ hai là lớn? – Luksprog