2012-03-15 10 views
12

Czy istnieje i każda metoda, aby stół jak html w android gdzie mogę ustawić szerokość kolumny wiersza, jak my w htmlJak ustawić szerokość stołu i liczba kolumn w TableLayout w Androidzie

<table > 
<tr><td style="width:'50%;"></td> 
    <td style="width:'50%;"></td> 
</tr> 
</table> 

<TableLayout 
      android:id="@+id/tableLayout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:stretchColumns="3" > 
      <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
      </TableRow> 
      <TableRow 
        android:id="@+id/tableRow1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" > 
      </TableRow> 
</TableLayout> 

Edit 1:

Na coś jak HTML tam pracach szerokość procent w odniesieniu do jednostki nadrzędnej

, ale waga wprowadzona w systemie Android działa w odniesieniu do rozmiaru ekranu dostępnego dla rootu .

Odpowiedz

22

Możesz użyć LinearLayout i atrybutu wagi, aby to osiągnąć.

<LinearLayout 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:weightSum="1.0"> 

Elementy podrzędne w wierszach każdy może mieć masę jako część sumy (jako procent). Pamiętaj, aby ustawić layout_width do "0DP"

<Button 
android:layout_height="wrap_content" 
android:layout_width="0dp" 
android:layout_weight="0.5" /> 

<Button 
android:layout_height="wrap_content" 
android:layout_width="0dp" 
android:layout_weight="0.5" /> 

Sprawdź ten poprzednie pytanie Linear Layout and weight in Android

16

TableLayout ma właściwości podobne do LinearLayout. Zrobiłem też trochę tego. Aby lepiej to powiedzieć, zobacz mój przykładowy kod poniżej. Mam nadzieję, że to się przyda.

<TableLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:stretchColumns="3" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1"> 
      <TextView android:text="@string/test1" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test2" android:layout_weight="1"></TextView> 
      <TextView android:text="@string/test3" android:layout_weight="1"></TextView> 
     </TableRow> 

    </TableLayout> 
+0

Numery kolumn są oparte na zera, więc stretchColumns powinno być 2 – user648026

Powiązane problemy