【问题标题】:How to set Table width and number of Columns in TableLayout in Android如何在 Android 的 TableLayout 中设置表格宽度和列数
【发布时间】:2012-03-31 22:34:06
【问题描述】:

有没有什么方法可以在android中制作像html这样的表格,我可以像在html中一样设置行列宽

<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 中引入的权重与可用的屏幕尺寸有关 根。

【问题讨论】:

    标签: xml android-layout android-widget


    【解决方案1】:

    您可以使用 LinearLayout 和 weight 属性来实现这一点。

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

    您的 Rows 中的每个子元素都可以作为总和的一部分(百分比)赋予权重。请务必将 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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 列号从零开始,所以stretchColumns 应该是2
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-02
      • 2014-05-24
      • 1970-01-01
      • 1970-01-01
      • 2011-09-23
      相关资源
      最近更新 更多