Android 布局详解 -三表格布局(TableLayout)以及重要属性

三表格布局(TableLayout)以及重要属性

 

          TableLayout跟TableRow 是一组搭配应用的布局,TableLayout置底,TableRow在TableLayout的上方,而Button、TextView等控件就在TableRow之上,别的,TableLayout之上也可以零丁放控件。TableLayout是一个应用错杂的布局,最简单的用法就仅仅是拖沓控件做出个界面,但实际上,会经常在代码里应用TableLayout,例如做出表格的结果。本文首要介绍TableLayout的根蒂根基应用办法。        

重要的几个属性如下:

 1.android:collapseColumns://隐藏指定的列
        ①设置 TableLayout 内的 TableRow 中需要隐藏的列的列索引,多个用“,”隔开 
        ②以第0行为序,隐藏指定的列:把android:collapseColumns=0,3 意思是把第0和第3列隐藏

 
    2.android:shrinkColumns://收缩指定的列以适合屏幕、不会挤出屏幕                                     ① 设置 TableLayout 内的 TableRow 中需要拉伸(该列会拉伸到所有可用空间)的列的列索引,多列个用“,”隔开(多列 每列填充空隙大小一样)
       ②以第0行为序,自动延伸指定的列填充可用部分: 当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用。
     ③设置了shrinkColumns=1,4,布局完全没有改变,因为LayoutRow里面还剩足够的空间。当LayoutRow布满控件时,设置了shrinkColumns=2,5,则控件自动向垂直方向填充空间 

 

                                                                                                                                         3.android:stretchColumns://尽量把指定的列表填充空白部分                                 

      ①设置 TableLayout 内的 TableRow 中需要收缩(为了使其他列不会被挤到屏幕 外,此列会自动收缩)的列的列索引,多个用“,”隔开                                                                                                                      

         ② 以第0行为序,尽量把指定的列填充空白部分:设置stretchColumns=2,5,第1,4列被尽量填充同时向右填充,直到2,5被压挤到最后边)。 

补充:

①表格布局的子对象不能指定 layout_width 属性.宽度永远是 MATCH_PARENT。

②不过子对象可以定义 layout_height 属性;其默认值是WRAP_CONTENT. 如果子对象是 TableRow,其高度永远是 WRAP_CONTENT

 

实例:

 

?
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
< 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_width = "wrap_content"
                 android:layout_height = "wrap_content"
                 android:layout_column = "2"
                 android:text = "填满剩余空白" >
             </ Button >
         </ TableRow >
     </ TableLayout >
 
</ LinearLayout >

 

转载于:https://www.cnblogs.com/tonglingqijie/p/4692950.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值