2011-12-09 32 views
26
<LinearLayout 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/linearLayout2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Street" 
      android:layout_gravity="left"/> 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="456546546" 
      android:layout_gravity="right" /> 

    </LinearLayout> 

</LinearLayout> 

Tôi đang cố gắng tạo bố cục với hai cột, với một chế độ xem văn bản ở bên trái và mặt khác ở bên phải. Tuy nhiên, các bản xem trước vẫn còn ở phía bên trái.Android: tạo hai cột trong linearlayout

Trả lời

74

Bạn nên sử dụng thuộc tính android:layout_weight. Dưới đây là một ví dụ:

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="Street" 
     android:layout_gravity="left" 
     android:background="#88FF0000"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_height="wrap_content" 
     android:text="456546546" 
     android:layout_gravity="right" 
     android:background="#8800FF00"/> 

</LinearLayout> 

enter image description here

+1

Có, công trình này cũng; có thể hoạt động tốt hơn nếu bạn cần cột đầy đủ hơn là chỉ văn bản ở bên trái và văn bản ở bên phải. –

1

Thử bố cục bảng khi thực hiện việc này. Về bố cục đồ họa Kéo bảng bố trí đặt các mục trong ô.

4

Vâng, điều này là khó hiểu. Mặc dù chiều rộng của LinearLayout được đặt thành fill_parent, nó vẫn chỉ mất chiều rộng tối thiểu cần thiết. Bạn cần phải thiết lập các TextView 2 đến fill_parent và sau đó lực hấp dẫn của nó sang phải:

<LinearLayout 
    android:id="@+id/linearLayout2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Street"/> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="456546546" 
     android:gravity="right" /> 

</LinearLayout> 
+0

Điều này tối ưu hóa tốt hơn một chút so với chỉ định "trọng lượng". – csi

3

Nếu u muốn có nhiều hàng trong mỗi cột u có thể sử dụng Table Layout

3

Tôi hy vọng nó sẽ hữu ích cho bạn.

Cố Bộ luật này ..

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


    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:background="#00FF00" 
     android:paddingRight="90dp" 
     android:orientation="vertical" > 

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b1" 
      android:text="Button 1"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#FF00FF" 
     android:orientation="vertical" >  

     <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/b2" 
      android:text="Button 2"/> 

    </LinearLayout> 

</LinearLayout> 
2

Và nếu bạn cần một khoảng cách giữa các nút:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <Button 

     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" 
     android:text="Via SMS" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.05" /> 

    <Button 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.95" 
     android:text="Diaporama" /> 
</LinearLayout> 
0

Một vấn đề tiềm năng với chỉ có hai LinearLayouts dọc cho bạn cột là không có gì đảm bảo hàng sẽ xếp hàng nếu chiều cao của hàng thay đổi. TableLayout là tốt nhất cho điều này, và cũng cung cấp cho bạn một loạt các điều khiển về cách cột thu nhỏ hoặc phát triển để lấp đầy không gian có sẵn.

Liên kết đã thay đổi kể từ @ santhosh-shettigar được đăng.
Hướng dẫn: https://developer.android.com/guide/topics/ui/layout/grid.html
tham khảo: http://developer.android.com/reference/android/widget/TableLayout.html

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