android的view的生命周期

来源:互联网 发布:淘宝导航条特效代码 编辑:程序博客网 时间:2024/05/22 03:32

总:android的view类是android的布局,组件的父类,了解view的生命周期,对于布局,组件的使用,以及自定义组件的构造将会有更好的了解。


(1)onAttachToWindow(onAttachedToWindow运行在onResume之后

     

     * This is called when the view is attached to a window.  At this point it
     * has a Surface and will start drawing.  Note that this function is
     * guaranteed to be called before {@link #onDraw(android.graphics.Canvas)},
     * however it may be called any time before the first onDraw -- including
     * before or after {@link #onMeasure(int, int)}.

(2)onMeasure(当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec).

     * Measure the view and its content to determine the measured width and the
     * measured height. This method is invoked by {@link #measure(int, int)} and
     * should be overriden by subclasses to provide accurate and efficient
     * measurement of their contents.

(3)onSizeChanged

      (1)会被View.java的方法private void sizeChange(int newWidth, int newHeight, int oldWidth, int oldHeight)调用

      (2)sizeChange会被View.java的方法setTop(int top)、setFrame(int left, int top, int right, int bottom)、setRight(int right)、setLeft(int left)、setBottom(int bottom)调用

     * This is called during layout when the size of this view has changed. If
     * you were just added to the view hierarchy, you're called with the old
     * values of 0.

(4)onLayout

       (1)是第二阶段的布局机制 (第一阶段是测量)

       (2)通过第一阶段获得的测量值,布置子元素的位置

     * In this phase, each parent calls
     * layout on all of its children to position them.
     * This is typically done using the child measurements
     * that were stored in the measure pass()


(7)onDraw

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

     * Called from layout when this view should
     * assign a size and position to each of its children.
     *
     * Derived classes with children should override
     * this method and call layout on each of
     * their children.
     * @param changed This is a new size or position for this view
     * @param left Left position, relative to parent
     * @param top Top position, relative to parent
     * @param right Right position, relative to parent
     * @param bottom Bottom position, relative to parent

(8)onDetachedFromWindow

   * This is called when the view is detached from a window.  At this point it
     * no longer has a surface for drawing.
     *

1 0
原创粉丝点击