这个就好像是 activity 的生命周期一样,如果我们要使用自定义的 view,那么就很有必要了解一下 view 的那些能够被重写的函数的执行顺序。废话不多讲,以常用的5个函数为例子,见下文:
1 package com.example.pulltorefreshtest; 2 3 import android.content.Context; 4 import android.graphics.Canvas; 5 import android.util.AttributeSet; 6 import android.util.Log; 7 import android.view.View; 8 9 /** 10 * Created by Administrator on 2015/7/12. 11 */ 12 public class testView extends View { 13 public testView(Context context, AttributeSet attrs) { 14 super(context, attrs); 15 } 16 17 @Override 18 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 19 Log.d("------","---onMeasure"); 20 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 21 } 22 23 @Override 24 protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 25 Log.d("------","---onLayout"); 26 super.onLayout(changed, left, top, right, bottom); 27 } 28 29 @Override 30 protected void onFinishInflate() { 31 Log.d("------","---onFinanshInflate"); 32 super.onFinishInflate(); 33 } 34 35 @Override 36 protected void onDraw(Canvas canvas) { 37 Log.d("------","---onDraw"); 38 super.onDraw(canvas); 39 } 40 41 @Override 42 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 43 Log.d("------","---onSizeChanged"); 44 super.onSizeChanged(w, h, oldw, oldh); 45 } 46 }
运行结果:
07-12 13:44:45.413 23734-23734/? D/------﹕ ---onFinanshInflate 07-12 13:44:45.443 23734-23734/? D/------﹕ ---onMeasure 07-12 13:44:45.493 23734-23734/? D/------﹕ ---onSizeChanged 07-12 13:44:45.493 23734-23734/? D/------﹕ ---onLayout 07-12 13:44:45.503 23734-23734/? D/------﹕ ---onMeasure 07-12 13:44:45.503 23734-23734/? D/------﹕ ---onLayout 07-12 13:44:45.503 23734-23734/? D/------﹕ ---onDraw