2010-01-20 42 views
128

Tôi đang bắt đầu với Android. Tôi gặp sự cố khi sắp xếp bố cục đơn giản.Căn chỉnh các bản xem trước văn bản ở cạnh trái và phải trong bố cục Android

Tôi muốn sử dụng LinearLayout để định vị hai TextViews trong một hàng. Một TextView ở phía bên tay trái, mặt còn lại ở phía bên phải (tương tự như float: left, float: ngay trong CSS).

Điều đó có thể xảy ra hay tôi cần sử dụng ViewGroup hoặc bố cục làm tổ khác để thực hiện nó?

Dưới đây là những gì tôi có cho đến nay:

<?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="fill_parent" 
android:orientation="horizontal" android:padding="10sp"> 
<TextView android:id="@+id/mytextview1" android:layout_height="wrap_content" android:text="somestringontheleftSomestring" android:layout_width="wrap_content"/> 
<TextView android:id="@+id/mytextview2" android:layout_height="wrap_content" android:ellipsize="end" 
    android:text="somestringontheright" android:layout_width="wrap_content"/> 
</LinearLayout> 

Trả lời

243

Sử dụng một RelativeLayout với layout_alignParentLeftlayout_alignParentRight:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="10dp"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:id="@+id/mytextview1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:id="@+id/mytextview2"/> 

</RelativeLayout> 

Ngoài ra, you should probably be using dip (or dp) rather than sp in your layout. sp phản ánh cài đặt văn bản cũng như mật độ màn hình để chúng thường chỉ để định kích thước các mục văn bản.

+45

Để ngăn chặn các nội dung từ chồng chéo, cũng có thể muốn thêm 'android: layout_toLeftOf = "@ + id/mytextview2"' đến TextView đầu tiên –

+0

Đối với các bảng năng động, bạn có thể làm: TableRow.LayoutParams col1Params = new TableRow.LayoutParams(); \t \t // Gói nội dung của hàng \t \t col1Params.height = LayoutParams.WRAP_CONTENT; \t \t col1Params.width = LayoutParams.WRAP_CONTENT; \t \t \t \t // Đặt trọng lực để tập trung mức độ nghiêm trọng của cột \t \t col1Params.gravity = Gravity.LEFT; – siddhusingh

+0

Cảm ơn bạn, bạn đã làm cho cuộc sống của tôi dễ dàng hơn. Thực sự thx .. – choz

80

Bạn có thể sử dụng thuộc tính trọng lực cho chế độ xem "nổi".

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:gravity="center_vertical|center_horizontal" 
      android:orientation="horizontal"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:layout_weight="1" 
      android:text="Left Aligned" 
      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="1" 
      android:text="Right Aligned" 
      /> 
    </LinearLayout> 

</LinearLayout> 
+1

Thật không may có vẻ như điều này không khiến cả hai mặt hàng trên cùng một dòng – Webnet

+1

Trọng lực không chỉ ảnh hưởng đến "căn chỉnh văn bản"? –

+0

tính năng này hiện không hoạt động. Có lẽ nó làm việc trong các API cũ hơn –

4

<TextView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:text="Hello world" /> 

<View 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Gud bye" /> 

+1

Điều gì là ........ – Subho

+0

cùng một bình luận Tôi muốn thực hiện: D: D;) – eRaisedToX

+0

Giải pháp này hoạt động trong nhiều trường hợp mà giải pháp được chấp nhận không – slezica

6

Ngay cả với mũi Rollin_s của, câu trả lời Dave Webb đã không làm việc cho tôi. Văn bản ở bên phải TextView vẫn chồng chéo văn bản ở bên trái TextView.

tôi cuối cùng đã nhận hành vi của tôi muốn có một cái gì đó như thế này:

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout01" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp"> 

<TextView 
    android:id="@+id/mytextview1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" /> 

<TextView 
    android:id="@+id/mytextview2" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/mytextview1" 
    android:gravity="right"/> 

</RelativeLayout> 

Lưu ý rằng mytextview2 có "android:layout_width" bộ như "match_parent".

Hy vọng điều này sẽ giúp ai đó!

0

Có thể thực hiện với LinearLayout (chi phí thấp hơn). Cung cấp cho chế độ xem thứ hai không gian còn lại để gravity có thể hoạt động. Kiểm tra lại API 16.

Proof

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Aligned left" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="end" 
     android:text="Aligned right" /> 
</LinearLayout> 

Nếu bạn muốn giới hạn kích thước của độ xem văn bản đầu tiên, làm như sau:

Điều chỉnh trọng lượng theo yêu cầu.

Proof 2

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Aligned left but too long and would squash the other view" /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:gravity="end" 
     android:text="Aligned right" /> 
</LinearLayout> 
Các vấn đề liên quan