5

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:

enter image description here

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:

enter image description here

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?

+1

Đặt một số bố trí cân –

+0

Hãy thử android: layout_weight = "1" cho cả hai – Android2390

+0

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

Trả lời

6

Tính năng này có hoạt động như bạn muốn không?

<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_right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/bubble_blue" 
    android:text="2" 
    android:gravity="center" > 
</TextView> 

<TextView 
    android:id="@+id/text_left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" 
    android:background="@drawable/bubble_purple" 
    android:text="9" 
    android:gravity="center" > 
</TextView> 
</LinearLayout> 

Bằng cách thêm tham số layout_weight và làm cho chúng bằng nhau, bạn yêu cầu bố cục bố trí bất kỳ phòng trống nào bằng nhau giữa hai chế độ xem. Nếu bạn muốn khối bên trái lớn hơn bên phải, bạn có thể cho nó một khối lượng lớn hơn.

Edit: Có lẽ đây là tốt hơn:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<LinearLayout 
    android:id="@+id/text_left" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.5" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#456" 
     android:text="5 + 1" 
     android:gravity="center" > 
    </TextView> 

</LinearLayout> 

<TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text=" = " > 
</TextView> 

<LinearLayout 
    android:id="@+id/text_right" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layoutDirection="rtl" 
    android:layout_weight="0.5"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#856" 
      android:text="6" 
      android:gravity="center" > 
     </TextView> 

</LinearLayout> 

</LinearLayout> 
+0

Không, không chính xác. Chắc chắn điều này làm việc cho văn bản dài nhưng đối với văn bản ngắn này [trải dài nền] (http://img225.imageshack.us/img225/8269/linearstretchedbg.png). Lý tưởng nhất, nền chỉ nên bọc văn bản và có khoảng trống giữa các bong bóng cho các tin nhắn ngắn, như trong hình thứ hai ở trên. – wollan

+0

Rất tuyệt! Gói chúng trong 'ViewGroups', không bao giờ nghĩ về điều đó. Tuy nhiên, tôi nhận được "Không tìm thấy mã định danh tài nguyên" cho 'android: layoutDirection'. Nhưng chuyển đổi bên trong 'LinearLayout' thành' FrameLayout' và thêm một 'android: layout_gravity =" right "' đã làm thủ thuật. Cám ơn! – wollan

Các vấn đề liên quan