xml 如何在Android中的TableLayout中设置表格宽度和列数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9719380/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 15:15:50  来源:igfitidea点击:

How to set Table width and number of Columns in TableLayout in Android

xmlandroid-layoutandroid-widget

提问by Trikaldarshi

Is there and any method to make table like html in android where i can set row column width as we do in html

有没有什么方法可以在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>


Edit 1:

编辑1:

For something like Html there the percentage width works with respect to its Parent

对于像 Html 这样的东西,百分比宽度适用于它的父级

but the weight introduced in Android works with respect to the screen size available for root.

但是 Android 中引入的权重与 root 可用的屏幕尺寸有关。

回答by user1267054

You can use a LinearLayout and the weight attribute to achive this.

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

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

The child elements in your Rows can each be given a weight as a portion of the sum (a percentage). Be sure to set the layout_width to "0dp"

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

Check out this previous question Linear Layout and weight in Android

查看之前的问题 Android 中的线性布局和权重

回答by razzy

The TableLayout has properties similar to LinearLayout. I did some tweek on it too. To better say it, see my sample code below. Hope its useful.

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>