在自定义view中打log,view的显示共有三种,visible、invisible和gone,分别看一下log:
onVisibilityChanged construct 2 parameters onFinishInflate onVisibilityChanged onVisibilityChanged onRtlPropertiesChanged onRtlPropertiesChanged onAttachedToWindow onWindowVisibilityChanged onWindowFocusChanged
onVisibilityChanged construct 2 parameters . onFinishInflate onVisibilityChanged onVisibilityChanged onRtlPropertiesChanged onRtlPropertiesChanged onAttachedToWindow onWindowVisibilityChanged onMeasure , width : 1080 ; height: 1557 onMeasure , width : 144 ; height: 1500 onSizeChanged onLayout --> l: 0 ; r : 144 ; t: 57 ; b: 201 : changed :true onMeasure , width : 1080 ; height: 1557 onMeasure , width : 144 ; height: 1500 onLayout --> l: 0 ; r : 144 ; t: 57 ; b: 201 : changed :false onWindowFocusChanged
construct 2 parameters . onFinishInflate onVisibilityChanged onVisibilityChanged onRtlPropertiesChanged onRtlPropertiesChanged onAttachedToWindow onWindowVisibilityChanged onMeasure , width : 1080 ; height: 1557 onMeasure , width : 144 ; height: 1500 onSizeChanged onLayout --> l: 0 ; r : 144 ; t: 57 ; b: 201 : changed :true onMeasure , width : 1080 ; height: 1557 onMeasure , width : 144 ; height: 1500 onLayout --> l: 0 ; r : 144 ; t: 57 ; b: 201 : changed :false onDraw onWindowFocusChanged
1、从中看到view默认为可见的,不是默认值时先调用 onVisibilityChanged ,但是此时该view的任何位置信息都不知道。
2、可见性改变后才是调用带有两个参数的构造函数
3、从xml 文件中 inflate 完成
4、将view 加到 window 中(View 是gone 的,那么View创建生命周期也就结束)
5、测量view的长宽(onMeasure)
6、定位View在父View中的位置(onLayout)--->(View 是invisible ,View 创建生命周期结束)
7、onDraw(只有可见的View 才在window 中绘制)
construct 1 parameter onVisibilityChanged onVisibilityChanged onRtlPropertiesChanged onRtlPropertiesChanged onAttachedToWindow onWindowVisibilityChanged onMeasure , width : 1080 ; height: 1557 onSizeChanged onLayout --> l: 0 ; r : 1080 ; t: 0 ; b: 1557 : changed :true onMeasure , width : 1080 ; height: 1557 onLayout --> l: 0 ; r : 1080 ; t: 0 ; b: 1557 : changed :false onDraw onWindowFocusChanged
默认情况下view的长和宽默认和父view的长和宽一致。
onWindowFocusChanged
onWindowVisibilityChanged
onDetachedFromWindow
View 的关键生命周期为
构造View --> onFinishInflate --> onAttachedToWindow --> onMeasure --> onSizeChanged --> onLayout --> onDraw --> onDetackedFromWindow