Android五大布局之一表格布局(TableLayout)

一.TableLayout(表格布局)常用属性:

android:collapseColumns:设置需要被隐藏的列的序号 
android:shrinkColumns:设置允许被收缩的列的列序号 
android:stretchColumns:设置运行被拉伸的列的列序号

以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = “2”,对应的是第三列! 
可以设置多个,用逗号隔开比如”0,2”,如果是所有列都生效,则用”*”号即可 
除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:

android:layout_column=”2”:表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的! 
android:layout_span=”4”:表示**合并**4个单元格,也就说这个组件占4个单元格

二.例子(简单做一个计算器的布局)

1.首先先创建一个TableLayout的XML文件

代码如下:

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="match_parent"
  4     android:stretchColumns="*"
  5     android:layout_height="match_parent" >
  6    
  7     <TextView 
  8         
  9         android:layout_weight="1"
 10         android:id="@+id/TextView1"
 11         android:layout_width="wrap_content"
 12         android:layout_height="60dp"
 13         android:gravity="right|center_vertical"
 14         android:textSize="30sp"
 15         android:text="90"
 16         />
 17     
 18     
 19     <TableRow
 20         android:id="@+id/tableRow1"
 21         android:layout_weight="1"
 22         android:layout_width="wrap_content"
 23         android:layout_height="match_parent" >
 24 
 25         <Button
 26             android:id="@+id/button1"
 27             android:layout_width="wrap_content"
 28             android:layout_height="match_parent"
 29                android:textSize="25sp"
 30             android:text="7" />
 31 
 32         <Button
 33             android:id="@+id/button2"
 34             android:layout_width="wrap_content"
 35                android:textSize="25sp"
 36             android:layout_height="match_parent"
 37             android:text="8" />
 38 
 39         <Button
 40             android:id="@+id/button3"
 41             android:layout_width="wrap_content"
 42                android:textSize="25sp"
 43             android:layout_height="match_parent"
 44             android:text="9" />
 45 
 46         <Button
 47             android:id="@+id/button4"
 48             android:layout_width="wrap_content"
 49             android:layout_height="match_parent"
 50                android:textSize="25sp"
 51             android:text="/" />
 52 
 53     </TableRow>
 54 
 55     <TableRow
 56         android:id="@+id/tableRow2"
 57         android:layout_weight="1"
 58         android:layout_width="wrap_content"
 59         android:layout_height="match_parent" >
 60 
 61         <Button
 62             android:id="@+id/button5"
 63                android:textSize="25sp"
 64             android:layout_width="wrap_content"
 65             android:layout_height="match_parent"
 66             android:text="4" />
 67 
 68         <Button
 69             android:id="@+id/button6"
 70                android:textSize="25sp"
 71             android:layout_width="wrap_content"
 72             android:layout_height="match_parent"
 73             android:text="5" />
 74 
 75         <Button
 76             android:id="@+id/button7"
 77                android:textSize="25sp"
 78             android:layout_width="wrap_content"
 79             android:layout_height="match_parent"
 80             android:text="6" />
 81 
 82         <Button
 83             android:id="@+id/button8"
 84                android:textSize="25sp"
 85             android:layout_width="wrap_content"
 86             android:layout_height="match_parent"
 87             android:text="*" />
 88 
 89     </TableRow>
 90 
 91     <TableRow
 92         android:id="@+id/tableRow3"
 93         android:layout_weight="1"
 94         android:layout_width="wrap_content"
 95         android:layout_height="match_parent" >
 96 
 97         <Button
 98             android:id="@+id/button9"
 99                android:textSize="25sp"
100             android:layout_width="wrap_content"
101             android:layout_height="match_parent"
102             android:text="1" />
103 
104         <Button
105             android:id="@+id/button10"
106                android:textSize="25sp"
107             android:layout_width="wrap_content"
108             android:layout_height="match_parent"
109             android:text="2" />
110 
111         <Button
112                android:textSize="25sp"
113             android:id="@+id/button11"
114             android:layout_width="wrap_content"
115             android:layout_height="match_parent"
116             android:text="3" />
117 
118         <Button
119             android:id="@+id/button12"
120                android:textSize="25sp"
121             android:layout_width="wrap_content"
122             android:layout_height="match_parent"
123             android:text="-" />
124 
125     </TableRow>
126 
127     <TableRow
128         android:id="@+id/tableRow4"
129         android:layout_weight="1"
130         android:layout_width="wrap_content"
131         android:layout_height="match_parent" >
132 
133         <Button
134             android:id="@+id/button13"
135             android:layout_width="wrap_content"
136             android:layout_height="match_parent"
137                android:textSize="25sp"
138             android:text="0" />
139 
140         <Button
141             android:id="@+id/button14"
142             android:layout_width="wrap_content"
143                android:textSize="25sp"
144             android:layout_height="match_parent"
145             android:text="." />
146 
147         <Button
148             android:id="@+id/button15"
149                android:textSize="25sp"
150             android:layout_width="wrap_content"
151             android:layout_height="match_parent"
152             android:text="+" />
153 
154         <Button
155             android:id="@+id/button16"
156                android:textSize="25sp"
157             android:layout_width="wrap_content"
158             android:layout_height="match_parent"
159             android:text="=" />
160 
161     </TableRow>
162 
163     <TableRow
164         android:id="@+id/tableRow5"
165         android:layout_weight="1"
166         android:layout_width="wrap_content"
167         android:layout_height="match_parent" >
168  
169         <Button
170             android:id="@+id/button17"
171             android:layout_span="4"
172                android:textSize="25sp"
173             android:layout_width="wrap_content"
174             android:layout_height="match_parent"
175             android:text="clear" />
176 
177     </TableRow>
178 
179 </TableLayout>

运行结果如下:

以上就是我对TableLayout(表格布局)理解

原文地址:https://www.cnblogs.com/zhaoyucong/p/6101353.html