2012-03-15 30 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