2011-07-05 42 views
9

Ngay bây giờ tôi có hai textViews một liên kết ở bên trái của bố cục tương đối và một căn chỉnh ở bên phải. Văn bản bên trái dài hơn nhiều so với bên phải. Trong một số trường hợp, văn bản bên trái là hai dòng. Khi điều này xảy ra, văn bản này chồng lên văn bản được căn chỉnh ở bên phải.Làm cách nào để ngăn các chế độ xem chồng chéo trong bố cục tương đối?

Có cách nào để ngăn điều này không? Hoặc có cách nào để nói nếu văn bản bên trái là hai dòng đặt văn bản ở bên phải trên dòng thứ hai?

Tôi đang gặp rắc rối với tên và thời gian xuống ở đây:

<RelativeLayout android:layout_width="wrap_content" android:id="@+id/relativeLayout" android:layout_height="wrap_content"> 

    <TextView android:text="TextView" android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10sp" android:textStyle="bold" android:textSize="16sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/address" android:text="address" android:layout_height="wrap_content" android:layout_below="@+id/name" android:layout_alignLeft="@+id/name" android:layout_marginLeft="30sp"></TextView> 
    <TextView android:layout_width="wrap_content" android:layout_toRightOf="@+id/address" android:text="" android:layout_height="wrap_content" android:layout_alignTop="@+id/address" android:layout_alignBottom="@+id/address" android:id="@+id/crossStreet"></TextView> 
    <TextView android:layout_width="wrap_content" android:id="@+id/time" android:text="Time" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10sp"></TextView> 

</RelativeLayout> 

Trả lời

16

bạn có thể thay vì chứa hai TextView trong một LinearLayout ngang (mà bản thân nó vẫn sẽ là một đứa trẻ của RelativeLayout của bạn). Sau đó, gán các thuộc tính trọng số thích hợp cho từng TextView trong LinearLayout đó.

+0

tuyệt vời. nó đã làm việc. Tôi cũng làm cho thời gian căn chỉnh ở bên phải của bố trí tuyến tính, để tham khảo trong tương lai. – mergesort

+0

Tôi có nên chơi với trọng lượng không? Tôi có 0,35 và .65 để làm cho nó hoạt động, nhưng .4 và .6 mang lại kết quả buồn cười – mergesort

0

Vui lòng kiểm tra mã bên dưới hữu ích hơn cho bạn.

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="wrap_content" 
    android:orientation="horizontal"> 
    <LinearLayout android:layout_width="fill_parent" 
     android:weightSum="1.0" android:id="@+id/relativeLayout" 
     android:layout_height="wrap_content"> 
     <TextView android:text="TextView1 hhhhh hhhhhhhh hhhhhhhh hhhhhhh hhhhhh" 
      android:id="@+id/name" android:layout_weight="0.5" 
      android:layout_width="0dip" android:layout_height="wrap_content" 
      android:layout_marginLeft="10sp" android:textStyle="bold" 
      android:textSize="16sp"></TextView> 
     <TextView android:layout_width="0dip" android:layout_weight="0.5" 
      android:id="@+id/time" android:text="Textview4" 
      android:layout_height="wrap_content" android:layout_alignParentRight="true" 
      android:layout_marginRight="10sp"></TextView> 
    </LinearLayout> 
</LinearLayout> 
19

Để có kết quả tương tự bằng cách sử dụng RelativeLayout, vui lòng sử dụng android:layout_toLeftOf="@+id/right_text". Nó sẽ định vị cạnh phải của khung nhìn này ở bên trái của ID xem neo đã cho.

Hãy làm theo ví dụ này:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/container" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingLeft="@dimen/margin_material_small" 
    android:paddingRight="@dimen/margin_material_small"> 
    <TextView 
     android:id="@+id/long_text" 
     style="@style/Base.TextAppearance.AppCompat.Title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some text field that will definitely overlap with another one" /> 
    <TextView 
     android:id="@+id/right_text" 
     style="@style/Base.TextAppearance.AppCompat.Display1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:singleLine="true" 
     tools:text="10.34" /> 
    <TextView 
     android:id="@+id/subtext" 
     style="@style/Base.TextAppearance.AppCompat.Medium" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/long_text" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/right_text" 
     android:singleLine="true" 
     tools:text="Some other text slightly smaller"/> 
</RelativeLayout> 


Kết quả bố trí trên:

Result of attached example

+7

Đây phải là câu trả lời được chấp nhận –

+1

Điều này chắc chắn là thích hợp hơn với câu trả lời hiện được chấp nhận. Không cần phải lồng một container khác (layout tuyến tính) bên trong một layout tương đối. Bạn muốn giảm container làm tổ càng nhiều càng tốt. – PentaPenguin

0
<RelativeLayout 
     android:id="@+id/relativeParent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <TextView 
      android:id="@+id/titleLeft" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:layout_toLeftOf="@+id/imageRight" 
      android:layout_alignParentStart="true" /> 

     <ImageView 
      android:id="@+id/imageRight" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentEnd="true" 
      android:src="@drawable/image" /> 
</RelativeLayout> 



android:layout_toLeftOf="@+id/imageRight" 
android:layout_alignParentStart="true" 

đã làm các trick mà không cần thêm thêm LinearLayout

0

Sửa chữa tốt nhất:

android:layout_toStartOf="@id/item_on_the_right" 
Các vấn đề liên quan