TableLayout(表格布局)

本文深入探讨TableLayout布局在Android开发中的应用,包括其基本属性、如何使用TableLayout实现不同布局需求,以及通过代码实例展示TableLayout的配置与效果。

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

表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象。TableRow可以添加子控件,每添加一个为一列。

TableLayout属性:

  android:collapseColumns:将TableLayout里面指定的列隐藏,若有多列需要隐藏,请用逗号将需要隐藏的列序号隔开。             

  android:stretchColumns:设置指定的列为可伸展的列,以填满剩下的多余空白空间,若有多列需要设置为可伸展,请用逗号将需要伸展的列序号隔开。                

  android:shrinkColumns:设置指定的列为可收缩的列。当可收缩的列太宽(内容过多)不会被挤出屏幕。当需要设置多列为可收缩时,将列序号用逗号隔开。

 列元素(Button)属性:(奇怪的是button 里面没有android:layout_column 和android:layout_span两个属性,写进去无反应,还不知道为什么)

  android:layout_colum:设置该控件在TableRow中指定的列

  android:layout_span:设置该控件所跨越的列数

图片:

代码:


  1 <</span>LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:layout_width="match_parent"
  4     android:layout_height="match_parent"
  5     android:orientation="vertical"
  6     tools:context=".AndroidTableLayoutActivity" >
  7 
  8     
  9 
 10     <</span>TableLayout
 11         android:id="@+id/tablelayout01"
 12         android:layout_width="match_parent"
 13         android:layout_height="wrap_content"
 14         android:shrinkColumns="1"
 15         android:stretchColumns="2" >
 16 
 17         
 18 
 19         <</span>Button
 20             android:id="@+id/btn01"
 21             android:layout_width="wrap_content"
 22             android:layout_height="wrap_content"
 23             android:text="独自一行" >
 24         </</span>Button>
 25 
 26         <</span>TableRow>
 27 
 28             <</span>Button
 29                 android:id="@+id/btn02"
 30                 android:layout_width="wrap_content"
 31                 android:layout_height="wrap_content"
 32                 android:text="普通" >
 33             </</span>Button>
 34 
 35             <</span>Button
 36                 android:id="@+id/btn03"
 37                 android:layout_width="wrap_content"
 38                 android:layout_height="wrap_content"
 39                 android:text="允许被收缩允许被收缩允许被收缩允许被收缩" >
 40             </</span>Button>
 41 
 42             <</span>Button
 43                 android:id="@+id/btn04"
 44                 android:layout_width="wrap_content"
 45                 android:layout_height="wrap_content"
 46                 android:text="允许被拉伸" >
 47             </</span>Button>
 48         </</span>TableRow>
 49     </</span>TableLayout>
 50     
 51 
 52     <</span>TableLayout
 53         android:id="@+id/tablelayout02"
 54         android:layout_width="match_parent"
 55         android:layout_height="wrap_content"
 56         android:collapseColumns="1" >
 57 
 58         <</span>TableRow>
 59 
 60             <</span>Button
 61                 android:id="@+id/btn05"
 62                 android:layout_width="wrap_content"
 63                 android:layout_height="wrap_content"
 64                 android:text="普通" >
 65             </</span>Button>
 66 
 67             <</span>Button
 68                 android:id="@+id/btn06"
 69                 android:layout_width="wrap_content"
 70                 android:layout_height="wrap_content"
 71                 android:text="被隐藏列" >
 72             </</span>Button>
 73 
 74             <</span>Button
 75                 android:id="@+id/btn07"
 76                 android:layout_width="wrap_content"
 77                 android:layout_height="wrap_content"
 78                 android:text="允许被拉伸" >
 79             </</span>Button>
 80         </</span>TableRow>
 81     </</span>TableLayout>
 82     
 83 
 84     <</span>TableLayout
 85         android:id="@+id/tablelayout03"
 86         android:layout_width="match_parent"
 87         android:layout_height="wrap_content"
 88         android:stretchColumns="1"
 89          >
 90 
 91         <</span>TableRow>
 92 
 93             <</span>Button
 94                 android:id="@+id/btn08"
 95                 android:layout_width="wrap_content"
 96                 android:layout_height="wrap_content"
 97                 android:text="普通" >
 98             </</span>Button>
 99 
100             <</span>Button
101                 android:id="@+id/btn09"
102                 android:layout_width="wrap_content"
103                 android:layout_height="wrap_content"
104                 android:text="填满剩余空白" >
105             </</span>Button>
106         </</span>TableRow>
107     </</span>TableLayout>
108     
109 
110     <</span>TableLayout
111         android:id="@+id/tablelayout04"
112         android:layout_width="match_parent"
113         android:layout_height="wrap_content"
114          >
115 
116         <</span>TableRow>
117 
118             <</span>Button
119                 android:id="@+id/btn10"
120                 android:layout_width="wrap_content"
121                 android:layout_height="wrap_content"
122                 android:text="普通" >
123             </</span>Button>
124             
125             <</span>Button
126                 android:id="@+id/btn11"
127                 android:layout_column="2"
128                 android:layout_width="wrap_content"
129                 android:layout_height="wrap_content"
130                 android:text="填满剩余空白" >
131             </</span>Button>
132         </</span>TableRow>
133     </</span>TableLayout>
134 </</span>LinearLayout>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值