This repository has been archived by the owner on Sep 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
android-viewhui-zhi-zhi-layoutguo-cheng.html
250 lines (202 loc) · 20.4 KB
/
android-viewhui-zhi-zhi-layoutguo-cheng.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Android View绘制之layout过程</title>
<link href="/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="华科美团点评技术俱乐部 Full Atom Feed" />
<link href="/feeds/android.atom.xml" type="application/atom+xml" rel="alternate" title="华科美团点评技术俱乐部 Categories Atom Feed" />
<!-- Bootstrap Core CSS -->
<link href="/theme/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="/theme/css/clean-blog.min.css" rel="stylesheet">
<!-- Code highlight color scheme -->
<link href="/theme/css/code_blocks/darkly.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<meta name="description" content="经过上一篇介绍的measure过程之后,各个View的尺寸信息已经存储在了每个View中,下面是layout过程,layout过程的目的是根据上一步中计算出的尺寸来正确设置各个View及其后代的位置。这个过程首先被调用的是View的layout()方法,layout()的方...">
<meta name="author" content="Di Wu">
<meta name="tags" content="android">
<meta name="tags" content="view">
<meta property="og:locale" content="zh_CN.UTF-8">
<meta property="og:site_name" content="华科美团点评技术俱乐部">
<meta property="og:type" content="article">
<meta property="article:author" content="/author/di-wu.html">
<meta property="og:url" content="/android-viewhui-zhi-zhi-layoutguo-cheng.html">
<meta property="og:title" content="Android View绘制之layout过程">
<meta property="article:published_time" content="2017-03-12 14:02:47+08:00">
<meta property="og:description" content="经过上一篇介绍的measure过程之后,各个View的尺寸信息已经存储在了每个View中,下面是layout过程,layout过程的目的是根据上一步中计算出的尺寸来正确设置各个View及其后代的位置。这个过程首先被调用的是View的layout()方法,layout()的方...">
<meta property="og:image" content="//images/bg.jpg">
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="/">华科美团点评技术俱乐部</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li><a href="/categories.html">分类</a></li>
<li><a href="/archives.html">归档</a></li>
<li><a href="/authors.html">作者</a></li>
<li><a href="/tags.html">标签</a></li>
<li><a href="/pages/about/index.html">关于</a></li>
<li><a href="/pages/friendlinks/index.html">友链</a></li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Header -->
<header class="intro-header" style="background-image: url('/images/bg.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1>Android View绘制之layout过程</h1>
<span class="meta">Posted by
<a href="/author/di-wu.html">Di Wu</a>
on 2017年 3月12日 周日
</span>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content -->
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<!-- Post Content -->
<article>
<p>经过<a href="http://www.viseator.xyz/2017/03/10/android_view_onMeasure/">上一篇</a>介绍的<code>measure</code>过程之后,各个<code>View</code>的尺寸信息已经存储在了每个<code>View</code>中,下面是<code>layout</code>过程,<code>layout</code>过程的目的是根据上一步中计算出的尺寸来正确设置各个<code>View</code>及其后代的位置。这个过程首先被调用的是<code>View</code>的<code>layout()</code>方法,<code>layout()</code>的方法签名是<code>public void layout(int l, int t, int r, int b)</code>,四个参数分别为左边界距父<code>View</code>左边界的距离,上边界距父<code>View</code>上边界的距离,右边界距父<code>View</code>左边界的距离,下边界距父<code>View</code>上边界的距离。</p>
<div class="highlight"><pre><span></span><span class="kt">boolean</span> <span class="n">changed</span> <span class="o">=</span> <span class="n">isLayoutModeOptical</span><span class="o">(</span><span class="n">mParent</span><span class="o">)</span> <span class="o">?</span>
<span class="n">setOpticalFrame</span><span class="o">(</span><span class="n">l</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">r</span><span class="o">,</span> <span class="n">b</span><span class="o">)</span> <span class="o">:</span> <span class="n">setFrame</span><span class="o">(</span><span class="n">l</span><span class="o">,</span> <span class="n">t</span><span class="o">,</span> <span class="n">r</span><span class="o">,</span> <span class="n">b</span><span class="o">);</span>
</pre></div>
<p><code>changed</code>是用于传递给<code>onLayout()</code>方法的参数,它指示了布局是否被改变。</p>
<p>后面的表达式查看了父<code>View</code>的布局模式是否需要显示边框,如需要,调用的是<code>setOpticalFrame()</code>方法:</p>
<div class="highlight"><pre><span></span><span class="kd">private</span> <span class="kt">boolean</span> <span class="nf">setOpticalFrame</span><span class="o">(</span><span class="kt">int</span> <span class="n">left</span><span class="o">,</span> <span class="kt">int</span> <span class="n">top</span><span class="o">,</span> <span class="kt">int</span> <span class="n">right</span><span class="o">,</span> <span class="kt">int</span> <span class="n">bottom</span><span class="o">)</span> <span class="o">{</span>
<span class="n">Insets</span> <span class="n">parentInsets</span> <span class="o">=</span> <span class="n">mParent</span> <span class="k">instanceof</span> <span class="n">View</span> <span class="o">?</span>
<span class="o">((</span><span class="n">View</span><span class="o">)</span> <span class="n">mParent</span><span class="o">).</span><span class="na">getOpticalInsets</span><span class="o">()</span> <span class="o">:</span> <span class="n">Insets</span><span class="o">.</span><span class="na">NONE</span><span class="o">;</span>
<span class="n">Insets</span> <span class="n">childInsets</span> <span class="o">=</span> <span class="n">getOpticalInsets</span><span class="o">();</span>
<span class="k">return</span> <span class="n">setFrame</span><span class="o">(</span>
<span class="n">left</span> <span class="o">+</span> <span class="n">parentInsets</span><span class="o">.</span><span class="na">left</span> <span class="o">-</span> <span class="n">childInsets</span><span class="o">.</span><span class="na">left</span><span class="o">,</span>
<span class="n">top</span> <span class="o">+</span> <span class="n">parentInsets</span><span class="o">.</span><span class="na">top</span> <span class="o">-</span> <span class="n">childInsets</span><span class="o">.</span><span class="na">top</span><span class="o">,</span>
<span class="n">right</span> <span class="o">+</span> <span class="n">parentInsets</span><span class="o">.</span><span class="na">left</span> <span class="o">+</span> <span class="n">childInsets</span><span class="o">.</span><span class="na">right</span><span class="o">,</span>
<span class="n">bottom</span> <span class="o">+</span> <span class="n">parentInsets</span><span class="o">.</span><span class="na">top</span> <span class="o">+</span> <span class="n">childInsets</span><span class="o">.</span><span class="na">bottom</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
<p>可以看到这个方法读取了设置的边框值, 把原值加上边框值后还是调用了<code>setFrame()</code>方法。</p>
<p><code>setFrame()</code>方法通过传入的参数确定了该<code>View</code>最终的位置以及尺寸。</p>
<p>可以看到,一个<code>View</code>最终显示在什么位置以及它的尺寸是由<code>layout()</code>方法决定的,<code>onMeasure()</code>方法只是将测量出的<code>View</code>期望具有的大小储存在<code>View</code>中。一般情况下,我们会根据储存的这个尺寸来作为设定的依据。</p>
<p>接下来<code>layout()</code>方法会调用<code>onLayout()</code>方法,(如果需要的话)我们需要重写这个方法来调用子<code>View</code>的<code>layout()</code>方法。所以决定子<code>View</code>如何显示的关键步骤就在这里,他们的位置和尺寸完全取决于这里调用它们的<code>layout()</code>方法时传入的参数。当然一般情况下我们会根据子<code>View</code>中的测量结果来设置这个值。这里拿<code>FrameLayout</code>这个需要处理子<code>View</code>的<code>ViewGroup</code>实例来举例:</p>
<div class="highlight"><pre><span></span><span class="nd">@Override</span>
<span class="kd">protected</span> <span class="kt">void</span> <span class="nf">onLayout</span><span class="o">(</span><span class="kt">boolean</span> <span class="n">changed</span><span class="o">,</span> <span class="kt">int</span> <span class="n">left</span><span class="o">,</span> <span class="kt">int</span> <span class="n">top</span><span class="o">,</span> <span class="kt">int</span> <span class="n">right</span><span class="o">,</span> <span class="kt">int</span> <span class="n">bottom</span><span class="o">)</span> <span class="o">{</span>
<span class="n">layoutChildren</span><span class="o">(</span><span class="n">left</span><span class="o">,</span> <span class="n">top</span><span class="o">,</span> <span class="n">right</span><span class="o">,</span> <span class="n">bottom</span><span class="o">,</span> <span class="kc">false</span> <span class="cm">/* no force left gravity */</span><span class="o">);</span>
<span class="o">}</span>
</pre></div>
<p>直接调用了<code>layoutChildren()</code>:(省略部分行)</p>
<div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">layoutChildren</span><span class="o">(</span><span class="kt">int</span> <span class="n">left</span><span class="o">,</span> <span class="kt">int</span> <span class="n">top</span><span class="o">,</span> <span class="kt">int</span> <span class="n">right</span><span class="o">,</span> <span class="kt">int</span> <span class="n">bottom</span><span class="o">,</span> <span class="kt">boolean</span> <span class="n">forceLeftGravity</span><span class="o">)</span> <span class="o">{</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">count</span> <span class="o">=</span> <span class="n">getChildCount</span><span class="o">();</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">parentLeft</span> <span class="o">=</span> <span class="n">getPaddingLeftWithForeground</span><span class="o">();</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">parentRight</span> <span class="o">=</span> <span class="n">right</span> <span class="o">-</span> <span class="n">left</span> <span class="o">-</span> <span class="n">getPaddingRightWithForeground</span><span class="o">();</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">parentTop</span> <span class="o">=</span> <span class="n">getPaddingTopWithForeground</span><span class="o">();</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">parentBottom</span> <span class="o">=</span> <span class="n">bottom</span> <span class="o">-</span> <span class="n">top</span> <span class="o">-</span> <span class="n">getPaddingBottomWithForeground</span><span class="o">();</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">count</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
<span class="kd">final</span> <span class="n">View</span> <span class="n">child</span> <span class="o">=</span> <span class="n">getChildAt</span><span class="o">(</span><span class="n">i</span><span class="o">);</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">width</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="na">getMeasuredWidth</span><span class="o">();</span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">height</span> <span class="o">=</span> <span class="n">child</span><span class="o">.</span><span class="na">getMeasuredHeight</span><span class="o">();</span>
<span class="kt">int</span> <span class="n">childLeft</span><span class="o">;</span>
<span class="kt">int</span> <span class="n">childTop</span><span class="o">;</span>
<span class="k">switch</span> <span class="o">(</span><span class="n">verticalGravity</span><span class="o">)</span> <span class="o">{</span>
<span class="k">case</span> <span class="n">Gravity</span><span class="o">.</span><span class="na">TOP</span><span class="o">:</span>
<span class="n">childTop</span> <span class="o">=</span> <span class="n">parentTop</span> <span class="o">+</span> <span class="n">lp</span><span class="o">.</span><span class="na">topMargin</span><span class="o">;</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">case</span> <span class="n">Gravity</span><span class="o">.</span><span class="na">CENTER_VERTICAL</span><span class="o">:</span>
<span class="n">childTop</span> <span class="o">=</span> <span class="n">parentTop</span> <span class="o">+</span> <span class="o">(</span><span class="n">parentBottom</span> <span class="o">-</span> <span class="n">parentTop</span> <span class="o">-</span> <span class="n">height</span><span class="o">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">+</span>
<span class="n">lp</span><span class="o">.</span><span class="na">topMargin</span> <span class="o">-</span> <span class="n">lp</span><span class="o">.</span><span class="na">bottomMargin</span><span class="o">;</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">case</span> <span class="n">Gravity</span><span class="o">.</span><span class="na">BOTTOM</span><span class="o">:</span>
<span class="n">childTop</span> <span class="o">=</span> <span class="n">parentBottom</span> <span class="o">-</span> <span class="n">height</span> <span class="o">-</span> <span class="n">lp</span><span class="o">.</span><span class="na">bottomMargin</span><span class="o">;</span>
<span class="k">break</span><span class="o">;</span>
<span class="k">default</span><span class="o">:</span>
<span class="n">childTop</span> <span class="o">=</span> <span class="n">parentTop</span> <span class="o">+</span> <span class="n">lp</span><span class="o">.</span><span class="na">topMargin</span><span class="o">;</span>
<span class="o">}</span>
<span class="n">child</span><span class="o">.</span><span class="na">layout</span><span class="o">(</span><span class="n">childLeft</span><span class="o">,</span> <span class="n">childTop</span><span class="o">,</span> <span class="n">childLeft</span> <span class="o">+</span> <span class="n">width</span><span class="o">,</span> <span class="n">childTop</span> <span class="o">+</span> <span class="n">height</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
</pre></div>
<p>省略了与获取布局属性相关的代码,可以看到:</p>
<ul>
<li>4-8行获取了父<code>View</code>的位置数据并在18-31行用于确定最终的位置数据</li>
<li>10-11行遍历了所有的子<code>View</code></li>
<li>12-13行获取了子<code>View</code>中在上一步骤的测量过程中储存的宽和高,并用于第33行中设置最终的右边界与下边界</li>
<li>第33行调用子<code>View</code>的<code>layout()</code>方法</li>
</ul>
</article>
<div class="tags">
<p>tags: <a href="/tag/android.html">android</a>, <a href="/tag/view.html">view</a></p>
</div>
<hr>
</div>
</div>
</div>
<hr>
<!-- Footer -->
<footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<ul class="list-inline text-center">
<li>
<a href="https://github.com/HUSTMeituanClub">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="mailto:@hustmeituan.club">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-envelope fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
</ul>
<p class="copyright text-muted">
Blog powered by <a href="http://getpelican.com">Pelican</a>,
which takes great advantage of <a href="http://python.org">Python</a>.
</p> </div>
</div>
</div>
</footer>
<!-- jQuery -->
<script src="/theme/js/jquery.min.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="/theme/js/bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="/theme/js/clean-blog.min.js"></script>
</body>
</html>