2011-12-09 13 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> 

Próbuję utworzyć układ z dwiema kolumnami, z jednym widokiem tekstowym po lewej stronie i drugim po prawej stronie. Jednak poglądy tekstowe nadal znajdują się po lewej stronie.Android: tworzenie dwóch kolumn w linelayout

Odpowiedz

74

Powinieneś użyć atrybutu android:layout_weight. Oto przykład:

<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

Tak, to działa tak samo; prawdopodobnie działa lepiej, jeśli potrzebujesz pełnych kolumn, a nie tylko tekstu po lewej i tekstu po prawej. –

1

Spróbuj tego układu tabeli. W układzie graficznym Przeciągnij układ tabeli Umieść elementy w komórce.

4

Tak, ten jest mylące. Mimo że szerokość LinearLayout jest ustawiona na fill_parent, nadal wymaga minimalnej szerokości. Trzeba ustawić 2nd TextView do fill_parent a następnie jego wagi do prawej:

<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

Optymalizuje to nieco lepiej niż określenie "waga". – csi

3

Jesli chcesz mieć wiele wierszy w każdej kolumny można korzystać Table Layout

3

Mam nadzieję, że to będzie pomocne dla Ciebie.

Spróbuj kod ..

<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

A jeśli trzeba lukę między przyciskami:

<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

Potencjalny problem z tylko o dwóch pionowych kolumnach LinearLayouts dla Ciebie jest to, że nic nie gwarantuje, wiersze będą uszeregować, jeśli wysokości rzędów są zmienne. TableLayout jest do tego najlepszy, a także zapewnia kontrolę nad tym, jak kolumny kurczą się lub rosną, aby wypełnić dostępną przestrzeń.

Link zmienił się od momentu opublikowania @ santhosh-shettigar.
Przewodnik: https://developer.android.com/guide/topics/ui/layout/grid.html
referencyjny: http://developer.android.com/reference/android/widget/TableLayout.html

Powiązane problemy