2012-03-15 22 views
12

设置表宽度和列数在TableLayout是否有任何方法,使表像Android的HTML,我可以设置行列宽度正如我们在HTML如何在Android的

<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> 

编辑1:

对于一些像HTML存在的百分比宽度工程关于其父

但Android中引入的权重与可用于 根的屏幕大小有关。

回答

22

您可以使用LinearLayout和weight属性来实现此目的。

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

行中的子元素可以分别作为总和(百分比)的一部分给予权重。一定要设置layout_width为 “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" /> 

看看这个先前的问题 Linear Layout and weight in Android

16

的TableLayout具有类似的LinearLayout性能。我也做了一些tweek。为了更好地说出来,请参阅下面的示例代码。希望它有用。

<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

列号是从零开始的,所以stretchColumns应该是2 – user648026 2014-02-25 04:03:44