Android表格布局(Table Layout)

Android表格布局(Table Layout)

 

先来看布局管理器之间继承关系图:


图1

可知TableLayout继承了LinearLayout,所以表格布局本质上依然是线性管理器。

 

表格布局采用行、列的形式来管理组件,它并不需要明确地声明包含了多少行、多少列,而是通过添加TableRow、其他组件来控制表格的行数和列数。

 

每向TableLayout添加一个TableRow,该TableRow就是一个表格行,TableRow也是容器,因此它也可以不断地添加组件,每添加一个子组件该表格就添加一列。

 

TableLayout一般以下面两种方式实现:

(1)  自己作为最顶层父容器

<!--定义一个TableLayout,有两行
   第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
   第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/TableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:shrinkColumns="1"
    android:stretchColumns="2">
   
   <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
   <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="独自一行的按钮1"/>
   
   <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
   <TableRow>
        <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="普通按钮1"/>
       
        <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被收缩的按钮1"/>
       
        <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被拉伸的按钮1"/>
   </TableRow>
   
   <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
   <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="独自一行的按钮2"/>
   
   <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
   <TableRow>
        <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="普通按钮2"/>
       
        <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被收缩的按钮2"/>
       
        <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被拉伸的按钮2"/>
   </TableRow>
   
</TableLayout>


效果如下:


图2

这里只有一个TableLayout,如果我们想单独控制地4行,比如想把“普通按钮2”隐藏,也就是增加android:collapseColumns="0",这样会把“普通按钮1”,这一列也隐藏了,如下图:


图3

但如果要实现只“普通按钮2”这列,我们来看下面的实现

 

(2)  LinearLayout作为TableLayout的容器

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">-->
 
<!--定义第1个TableLayout,有两行
   第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
   第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout
    android:id="@+id/TableLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:shrinkColumns="1"
    android:stretchColumns="2">
   
   <!--这是此TableLayout的第1行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
   <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="独自一行的按钮1"/>
   
   <!-- 这是第2行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
   <TableRow>
        <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="普通按钮1"/>
       
        <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被收缩的按钮1"/>
       
        <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被拉伸的按钮1"/>
   </TableRow>
   
   
   
</TableLayout>
<!--定义第2个TableLayout,有两行
   第1列所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度
   第2列所有单元格的宽度可以拉伸,以保证能完全填满表格空余空间-->
<TableLayout
    android:id="@+id/TableLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:collapseColumns="0"
    android:shrinkColumns="1"
    android:stretchColumns="2">
   
   <!--这是此TableLayout的第3行,没有使用TableRow,直接添加一个Button,那么次Button自己占用整行 -->
   <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="独自一行的按钮2"/>
   
   <!-- 这是第4行,先添加一个TableRow,并为TableRow添加三个Button,也就是此行包含三列 -->
   <TableRow>
        <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="普通按钮2"/>
       
        <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被收缩的按钮2"/>
       
        <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="被拉伸的按钮2"/>
   </TableRow>
   
   </TableLayout>
  
</LinearLayout>


效果如下:


图4

 

通过在第2个TableLayout中增加android:collapseColumns="0"实现,这里需要主要的是LinearLayout的android:orientation属性值的设置,如果没有这一项或是其值为horizontal,那么后面两行都看不到,因为是以水平方向排列的,后面两行显示在前两行的右边,看不到。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

loongembedded

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值