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(表格布局)理解

时间: 2024-08-10 21:15:25

Android五大布局之一表格布局(TableLayout)的相关文章

Android基础_2 Activity线性布局和表格布局

在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是练习android的界面设计.参考的资料为mars老师的教程. 线性布局: 线性布局就是将各种控件按照行或者列依次进行排列. 其中本实验用到的各控件的属性解释如下: android:layout_weight属性是指不同的控件在activity中占有体积大小的比例. android:paddingL

Android——布局(线性布局linearLayout,表格布局TableLayout,帧布局FrameLayout)

线性布局: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent&q

Android布局之表格布局TableLayout

一.TableLayout概述 TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象 二.TableLayout的全局属性  android:collapseColumns = "1,2"      隐藏从0开始索引列,列直接必须ongoing逗号隔开:1,2,5 android:shrikColumns = "1,2"       收缩从0开始的索引列.当可收缩的列太宽(内容过多)不会被挤出屏

Android布局_表格布局TableLayout

一.TableLayout概述 TableLayout表格布局模型以行列的形式管理子控件,每一行为一个TableRow的对象,当然也可以是一个View的对象 二.TableLayout的全局属性  1.android:collapseColumns = "1,2" 隐藏从0开始索引列,列直接必须ongoing逗号隔开:1,2,5 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android&

android——相对布局,表格布局

1.相对布局 RelativeLayout 又称作相对布局,也是一种非常常用的布局.和LinearLayout 的排列规则不同,RelativeLayout 显得更加随意一些,它可以通过相对定位的方式让控件出现在布局的任何位置.也正因为如此,RelativeLayout 中的属性非常多,不过这些属性都是有规律可循的,其实并不难理解和记忆. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android&qu

1.线性布局、相对布局、表格布局、帧布局

线性布局(Linearlayout): 有顺序.有次序.有条理的摆放着...(这是我的理解,或者说是我的表达方式) 分垂直摆放(Vertical)和平行摆放(horizontal) 相对布局(Relativelayout): 所有控件都要给id,然后通过上一个控件的id来确定下一个控件的相对位置 表格布局(Tablelayout): 很少用到 帧布局(Framelayout): 平时也很少用到,但到今天突然想到可以用来做那个置顶按钮

Android:控件布局(表格布局)TableLayout

摘录自:http://www.cnblogs.com/tinyphp/p/3812486.html TableLayout继承LinearLayout 有多少个TableRow对象就有多少行, 列数等于最多子控件的TableRow的列数 直接在TableLayout加控件,控件会占据一行 TableLayout属性(也叫全局属性):*代表所有列 android:shrinkColumns -------设置可收缩的列,(内容过多,则收缩,扩展到第二行,控件没布满TableLayout时不起作用)

帧布局和表格布局

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Text

浅谈Android五大布局(二)——RelativeLayout和TableLayout

在浅谈Android五大布局(一)中已经描述了LinearLayout(线性布局).FrameLayout(单帧布局)和AbsoulteLayout(绝对布局)三种布局结构,剩下的两种布局RelativeLayout(相对布局)和TableLayout(表格布局)相对之前布局结构稍显复杂一点,所以这里另起篇幅进行介绍. RelativeLayout: RelativeLayout按照各子元素之间的位置关系完成布局.在此布局中的子元素里与位置相关的属性将生效.例如android:layout_be