Duan

The more hard, the more fortunate.


  • 首页

  • 关于

  • 归档

  • 标签

Android:的布局管理器(下篇)-GridLayout、AbsoluteLayout、android单位转换

线性布局,表格布局、帧布局和相对布局的介绍参看上一篇博文:
Android的布局管理器(上篇)

  • 网格布局
  • 绝对布局

网格布局

  1. 网格布局由GridLayout类表示,它是Android4.0新增的布局管理器。如果希望在更早的Android平台上使用该布局管理器,则需要导入相应支撑库。
  2. GridLayout的作用类似HTML中的table标签,它把整个容器划分成rows $\times$Columns个网格,每个网格可以放置一个组件,除此之外也可以设置一个组件横跨多少列,一个组件纵跨多少行。

    参考:浅谈android4.0开发之GridLayout布局
    如果需要设置某控件跨越多行或多列,只需将该子控件的android:layout_rowSpan或者layout_columnSpan属性设置为数值,再设置其layout_gravity属性为fill即可,前一个设置表明该控件跨越的行数或列数,后一个设置表明该控件填满所跨越的整行或整列。

GridLayout的常用属性
  • android:alignmentMode 设置该布局管理器采用的对齐模式
  • android:columnCount 设置该网格的列数量
  • android:columnOrderPreserved 设置该网格布局管理器是否保留列序号
  • android:rowCount 设置该网格的行数目
  • android:rowOrderPreserved 设置该网格布局管理器是否保留行序号
  • android:useDefaultMargins 设置布局管理器是否使用默认的页边距
GridLayout.LayoutParams常用属性
  • android:layout_column 设置该子组件在GridLayout的第几列
  • android:layout_columnSpan 设置该子组件在GridLayout横向上跨几行
  • android:layout_gravity 设置该子组件采用何种方式占据该网格的空间
  • android:layout_row 设置该子组件在GridLayout的第几行
  • android:layout_rowSpan 设置该子组件在GridLayout纵向上跨几行
xml示例
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<GridLayout
android:columnCount="4"
android:rowCount="7"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:paddingBottom="30dp"
android:paddingLeft="16dp"
android:paddingTop="30dp"
android:text="22 * 10"
android:textSize="30sp"
android:textStyle="bold" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_gravity="right"
android:layout_margin="5dp"
android:text="clear" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#fff8f8"
android:text="7" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#fde3e3"
android:text="8" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#ffd9d9"
android:text="9" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="2"
android:background="#ffc6c6"
android:text="/" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#fff8f8"
android:text="4" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ffdbfb"
android:text="5" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ffc5f9"
android:text="6" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="3"
android:background="#ff96f3"
android:text="*" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="left"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fff8f8"
android:text="1" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fdcdce"
android:text="2" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#ffbdbe"
android:text="3" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:layout_gravity="right"
android:layout_margin="5dp"
android:layout_row="4"
android:background="#fd8789"
android:text="-" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_row="5"
android:layout_columnSpan="2"
android:layout_gravity="fill"
android:layout_margin="5dp"
android:background="#d9eaff"
android:text="0" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:layout_margin="5dp"
android:layout_row="5"
android:background="#bbd9ff"
android:text="." />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textColor="#585858"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:layout_column="0"
android:layout_margin="5dp"
android:layout_row="6"
android:background="#7bedff"
android:text="=" />
<Button
style="@style/Base.Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_column="3"
android:layout_gravity="fill"
android:layout_margin="5dp"
android:layout_row="5"
android:background="#7dcfff"
android:text="+" />
</GridLayout>
</LinearLayout>

截图

绝对布局

  1. 绝对布局由AbsoluteLayout表示,对于AbsoluteLayout布局Android不提供任何控制,而是由开发人员自己用过X坐标和Y坐标来控制组件的位置,以及其他的一些属性。
  2. 大部分时候,使用绝对布局都不是一个好思路,因为运行Android应用的手机往往千差万别,因此屏幕分辨率,大小都可能存在较大差异,使用绝对布局会很难兼顾不同屏幕分辨率,大小不同的问题,因此AbsoluteLayout已经过时。
xml属性

android:layout_x 指定该子组件的x坐标
android:layout_y 指定该子组件的y坐标

Android单位与换算

详细可参考:关于 Android 适配,看完这篇文章就够了

单位
  • px(像素):每一个px对应屏幕上的一点
  • in:表示英寸,每英寸相当于2.54厘米
  • dpi(屏幕像素密度):每英寸的像素点数比如120dpi、160dpi等,其值与屏幕尺寸和分辨率有关
  • dp/dip(设备独立像素):密度无关像素,以160dpi为基准。如求xdip对应的像素值y:
    $\frac{dpi}{160}\times x=y$
案例
一个手机屏幕分辨率为480*800,屏幕尺寸为3.7in。求其dpi?在布局中宽设为320dp时相当于多少px?
  • dpi:$ \frac{\sqrt{480^2+800^2}}{3.7} =252dpi$

  • 320dp:$\frac{252}{160}\times320=504dp $

然而手机屏幕才480px,算出的结果却是504px!


注意:
手机上面计算dpi为理论值,实际上只有120(low),160(medium),240(high),320(xhigh)等几种,因此实际的计算公式为:320$\times\frac{240}{160}=480px$,与屏幕宽度相同,刚好占据整个屏幕。

END
James Duan

James Duan

¥.¥

23 日志
43 标签
Github Twitter QQ CSDN email
© Thu May 18 2017 08:00:00 GMT+0800 (中国标准时间) - 2017 James Duan
由 Hexo 强力驱动
主题 - NexT.Mist