当前位置: 首页 > news >正文

Android中layout过程详解

    相比较onMeasure ,layout过程要简单多了,正如layout的中文意思“布局”中表达的一样,layout的过程就是确定View在屏幕上显示的具体位置,在代码中就是设置其成员变量mLeft,mTop,mRight,mBottom的值,这几个值构成的矩形区域就是该View显示的位置,不过这里的具体位置都是相对与父视图的位置。

    与onMeasure过程类似,ViewGroup在onLayout函数中通过调用其children的layout函数来设置子视图相对与父视图中的位置,具体位置由函数layout的参数决定,当我们继承ViewGroup时必须重载onLayout函数(ViewGroup中onLayout是abstract修饰),然而onMeasure并不要求必须重载,因为相对与layout来说,measure过程并不是必须的,具体后面会提到。首先我们来看下View.java中函数layout和onLayout的源码:

复制代码
public void layout(int l, int t, int r, int b) {int oldL = mLeft;int oldT = mTop;int oldB = mBottom;int oldR = mRight;boolean changed = setFrame(l, t, r, b);if (changed || (mPrivateFlags & LAYOUT_REQUIRED) == LAYOUT_REQUIRED) {if (ViewDebug.TRACE_HIERARCHY) {ViewDebug.trace(this, ViewDebug.HierarchyTraceType.ON_LAYOUT);}onLayout(changed, l, t, r, b);mPrivateFlags &= ~LAYOUT_REQUIRED;ListenerInfo li = mListenerInfo;if (li != null && li.mOnLayoutChangeListeners != null) {ArrayList<OnLayoutChangeListener> listenersCopy =(ArrayList<OnLayoutChangeListener>)li.mOnLayoutChangeListeners.clone();int numListeners = listenersCopy.size();for (int i = 0; i < numListeners; ++i) {listenersCopy.get(i).onLayoutChange(this, l, t, r, b, oldL, oldT, oldR, oldB);}}}mPrivateFlags &= ~FORCE_LAYOUT;}
复制代码

函数layout的主体过程还是很容易理解的,首先通过调用setFrame函数来对4个成员变量(mLeft,mTop,mRight,mBottom)赋值,然后回调onLayout函数,最后回调所有注册过的listener的onLayoutChange函数。

 对于View来说,onLayout只是一个空实现,一般情况下我们也不需要重载该函数:

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {}

接着我们来看下ViewGroup.java中layout的源码:

复制代码
public final void layout(int l, int t, int r, int b) {if (mTransition == null || !mTransition.isChangingLayout()) {super.layout(l, t, r, b);} else {// record the fact that we noop'd it; request layout when transition finishesmLayoutSuppressed = true;}}
复制代码

 super.layout(l, t, r, b)调用的即是View.java中的layout函数,相比之下ViewGroup增加了LayoutTransition的处理,LayoutTransition是用于处理ViewGroup增加和删除子视图的动画效果,也就是说如果当前ViewGroup未添加LayoutTransition动画,或者LayoutTransition动画此刻并未运行,那么调用super.layout(l, t, r, b),继而调用到ViewGroup中的onLayout,否则将mLayoutSuppressed设置为true,等待动画完成时再调用requestLayout()。

      上面super.layout(l, t, r, b)会调用到ViewGroup.java中onLayout,其源码实现如下:
 
    @Overrideprotected abstract void onLayout(boolean changed,int l, int t, int r, int b);

和前面View.java中的onLayout实现相比,唯一的差别就是ViewGroup中多了关键字abstract的修饰,也就是说ViewGroup类只能用来被继承,无法实例化,并且其子类必须重载onLayout函数,而重载onLayout的目的就是安排其children在父视图的具体位置。重载onLayout通常做法就是起一个for循环调用每一个子视图的layout(l, t, r, b)函数,传入不同的参数l, t, r, b来确定每个子视图在父视图中的显示位置。
      那layout(l, t, r, b)中的4个参数l, t, r, b如何来确定呢?联想到之前的measure过程,measure过程的最终结果就是确定了每个视图的mMeasuredWidth和mMeasuredHeight,这两个参数可以简单理解为视图期望在屏幕上显示的宽和高,而这两个参数为layout过程提供了一个很重要的依据(但不是必须的),为了说明这个过程,我们来看下LinearLayout的layout过程:

 

复制代码
void layoutVertical() {……for (int i = 0; i < count; i++) {final View child = getVirtualChildAt(i);if (child == null) {childTop += measureNullChild(i);} else if (child.getVisibility() != GONE) {final int childWidth = child.getMeasuredWidth();final int childHeight = child.getMeasuredHeight();……setChildFrame(child, childLeft, childTop + getLocationOffset(child),childWidth, childHeight);childTop += childHeight + lp.bottomMargin + getNextLocationOffset(child);i += getChildrenSkipCount(child, i);}}}
private void setChildFrame(View child, int left, int top, int width, int height) {        child.layout(left, top, left + width, top + height);}
复制代码

 

从setChildFrame可以看到LinearLayout中的子视图的右边界等于left + width,下边界等于top+height,也就是说在LinearLayout中其子视图显示的宽和高由measure过程来决定的,因此measure过程的意义就是为layout过程提供视图显示范围的参考值。

      layout过程必须要依靠measure计算出来的mMeasuredWidth和mMeasuredHeight来决定视图的显示大小吗?事实并非如此,layout过程中的4个参数l, t, r, b完全可以由视图设计者任意指定,而最终视图的布局位置和大小完全由这4个参数决定,measure过程得到的mMeasuredWidth和mMeasuredHeight提供了视图大小的值,但我们完全可以不使用这两个值,可见measure过程并不是必须的。\\
      说到这里就不得不提getWidth()、getHeight()和getMeasuredWidth()、getMeasuredHeight()这两对函数之间的区别,getMeasuredWidth()、getMeasuredHeight()返回的是measure过程得到的mMeasuredWidth和mMeasuredHeight的值,而getWidth()和getHeight()返回的是mRight - mLeft和mBottom - mTop的值,看View.java中的源码便一清二楚了:

 

public final int getMeasuredWidth() {return mMeasuredWidth & MEASURED_SIZE_MASK;}
public final int getWidth() {return mRight - mLeft;}

这也解释了为什么有些情况下getWidth()和getMeasuredWidth()以及getHeight()和getMeasuredHeight()会得到不同的值。

 
      总结:整个layout过程比较容易理解,一般情况下layout过程会参考measure过程中计算得到的mMeasuredWidth和mMeasuredHeight来安排子视图在父视图中显示的位置,但这不是必须的,measure过程得到的结果可能完全没有实际用处,特别是对于一些自定义的ViewGroup,其子视图的个数、位置和大小都是固定的,这时候我们可以忽略整个measure过程,只在layout函数中传入的4个参数来安排每个子视图的具体位置。
http://www.xdnf.cn/news/820999.html

相关文章:

  • 网络 || 科来网络分析系统
  • 0磁道损坏的硬盘如何修复?
  • 分享70个Java源码总有一个是你想要的
  • Linux的安装与配置(保姆级教学)
  • Servlet--HttpServletRequest获取请求信息(请求头、请求行、参数)详解
  • Linux网络命令详解
  • 05 ORM
  • 天龙八部单机版(兽血沸腾):大背包天机华裳格武道二
  • root权限获取排行榜,root权限软件排行榜
  • 电子产品硬件开发流程
  • update set命令用来修改表中的数据
  • DataFormatString,DataTextFormatString
  • php钓鱼网站怎么做视频教程,反黑钓鱼网站全过程
  • 5款常用的漏洞扫描工具,网安人员不能错过!
  • 原码反码补码
  • Android开发之修改系统开机Logo与开机动画
  • Windows 7 杜比软件安装
  • 如何快速搭建网站(小白教程)(48小时内完成)
  • 【广工考试笔记】计算机网络考试速成笔记
  • 搜索引擎工作原理
  • 通过IP地址和子网掩码与运算计算相关地址
  • Java如何配置环境变量
  • 基于量子粒子群算法(QPSO)优化LSTM的风电、负荷等时间序列预测算法(Matlab代码实现)
  • JAVA正则表达式语法大全
  • OKCC外呼中心配置的电话系统规则
  • 基于粒子群优化 (PSO) 对太阳能电池板进行最大功率点跟踪(MPPT)(Matlab代码实现)
  • 【深度学习】使用d2l包和相关环境配置的一些血泪心得
  • 远程访问 client and server
  • 图像处理----lena图像的由来
  • HG255D[OpenWrt]刷机