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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM