表格布局模型以行列的形式管理子控件,每一行為一個tablerow的對象,當然也可以是一個view的對象。tablerow可以添加子控件,每添加一個為一列。
tablelayout屬性:
android:collapsecolumns:將tablelayout里面指定的列隱藏,若有多列需要隱藏,請用逗號將需要隱藏的列序號隔開。
android:stretchcolumns:設置指定的列為可伸展的列,以填滿剩下的多余空白空間,若有多列需要設置為可伸展,請用逗號將需要伸展的列序號隔開。
android:shrinkcolumns:設置指定的列為可收縮的列。當可收縮的列太寬(內(nèi)容過多)不會被擠出屏幕。當需要設置多列為可收縮時,將列序號用逗號隔開。
列元素(button)屬性:(奇怪的是button 里面沒有android:layout_column 和android:layout_span兩個屬性,寫進去無反應,還不知道為什么)
android:layout_colum:設置該控件在tablerow中指定的列。
android:layout_span:設置該控件所跨越的列數(shù)。
圖片:
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/tools" android:layout_width= "match_parent" android:layout_height= "match_parent" android:orientation= "vertical" tools:context= ".androidtablelayoutactivity" > <!-- 定義第一個表格,指定第 2 列允許收縮,第 3 列允許拉伸 --> <tablelayout android:id= "@+id/tablelayout01" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:shrinkcolumns= "1" android:stretchcolumns= "2" > <!-- 直接添加按鈕,自己占用一行 --> <button android:id= "@+id/btn01" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "獨自一行" > </button> <tablerow> <button android:id= "@+id/btn02" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "普通" > </button> <button android:id= "@+id/btn03" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "允許被收縮允許被收縮允許被收縮允許被收縮" > </button> <button android:id= "@+id/btn04" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "允許被拉伸" > </button> </tablerow> </tablelayout> <!-- 定義第 2 個表格,指定第 2 列隱藏 --> <tablelayout android:id= "@+id/tablelayout02" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:collapsecolumns= "1" > <tablerow> <button android:id= "@+id/btn05" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "普通" > </button> <button android:id= "@+id/btn06" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "被隱藏列" > </button> <button android:id= "@+id/btn07" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "允許被拉伸" > </button> </tablerow> </tablelayout> <!-- 定義第 3 個表格,指定第 2 列填滿空白--> <tablelayout android:id= "@+id/tablelayout03" android:layout_width= "match_parent" android:layout_height= "wrap_content" android:stretchcolumns= "1" > <tablerow> <button android:id= "@+id/btn08" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "普通" > </button> <button android:id= "@+id/btn09" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "填滿剩余空白" > </button> </tablerow> </tablelayout> <!-- 定義第 3 個表格,指定第 2 列橫跨 2 列--> <tablelayout android:id= "@+id/tablelayout04" android:layout_width= "match_parent" android:layout_height= "wrap_content" > <tablerow> <button android:id= "@+id/btn10" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "普通" > </button> <button android:id= "@+id/btn11" android:layout_column= "2" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "填滿剩余空白" > </button> </tablerow> </tablelayout> </linearlayout> |
希望本文所述對大家學習android軟件編程有所幫助。