TableLayout学习总结!

本文详细介绍了TableLayout的基本用法和高级特性,包括如何设置列宽拉伸、列隐藏及列收缩等,通过具体示例帮助读者更好地理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

TableLayout,顾名思义,代表表格布局。类似HTML编程中的<table>。在某些布局时较优越。
本人不经常使用,在登录表单页面的编写时,偶尔会涉及到。

因为用的不多,所以不熟。今天挑出来专门研究下。


在xml中,一般都是这种结构进行使用:

<TableLayout>
        <TableRow>
            <view />
        </TableRow>    
</TableLayout>

TableLayout的优越性之一就是可以在TableLayout属性下控制拉伸,而<TableRow>则表示一行。比如:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="2"
        >
        <TableRow>
            <Button android:hint="helloworld"/>
            <Button android:hint="helloworld"/>
            <Button android:hint="helloworld"/>
        </TableRow>
        
 </TableLayout>

效果如图:



也可以改成android:stretchColumns="0,2",这时候代表第一列和第三列拉伸。



在LinearLayout下有layout_weight属性来进行拉伸控制,在这里,也可以不使用stretchColumns,使用weight控制。比如:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <TableRow>
            <Button android:hint="helloworld"
                android:layout_weight="1"/>
            <Button android:hint="helloworld"/>
            <Button android:hint="helloworld"
                android:layout_weight="1"/>
        </TableRow>
        
    </TableLayout>

效果如图:


和第二个例子一模一样,只是代码的书写量变多了,也体现了程序员的技术水平。


collapseColumns的属性使用:

编写如下代码:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2"
        android:collapseColumns="1"
        >
        <TableRow>
            <Button android:hint="helloworld0"/>
            <Button android:hint="helloworld1"/>
            <Button android:hint="helloworld2"/>
            <Button android:hint="helloworld3"/>
        </TableRow>
        
    </TableLayout>

效果如图:


collapseColumns表示第几列缩进。


android:shrinkColumns的使用:

编写如下代码:

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:stretchColumns="1,2"
        android:collapseColumns="1"
        >
        <TableRow>
            <Button android:hint="helloworld0222222222222222222222222222222"/>
            <Button android:hint="helloworld1333333333333333333333333333333"/>
            <Button android:hint="helloworld2444444444444444444444444444444"/>
            <Button android:hint="helloworld3444444444444444444444444444444"/>
        </TableRow>
        
    </TableLayout>

效果如图:


可以看到有些button已经被挤出来了。

这时候使用shrinkColumns属性:

<pre name="code" class="html"><TableLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="0,1,2,3"
        >
        <TableRow>
            <Button android:hint="helloworld0222222222222222222222222222222"/>
            <Button android:hint="helloworld1333333333333333333333333333333"/>
            <Button android:hint="helloworld2444444444444444444444444444444"/>
            <Button android:hint="helloworld3444444444444444444444444444444"/>
        </TableRow>
        
    </TableLayout>



让页面显示更多view,如图示:


掌握这么多,我猜基本上TableLayout就差不多可以了吧。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值