android view 中各函数的执行顺数

时间:2023-03-08 23:27:21
android view 中各函数的执行顺数

这个就好像是 activity 的生命周期一样,如果我们要使用自定义的 view,那么就很有必要了解一下 view 的那些能够被重写的函数的执行顺序。废话不多讲,以常用的5个函数为例子,见下文:

 package com.example.pulltorefreshtest;

 import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; /**
* Created by Administrator on 2015/7/12.
*/
public class testView extends View {
public testView(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d("------","---onMeasure");
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} @Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
Log.d("------","---onLayout");
super.onLayout(changed, left, top, right, bottom);
} @Override
protected void onFinishInflate() {
Log.d("------","---onFinanshInflate");
super.onFinishInflate();
} @Override
protected void onDraw(Canvas canvas) {
Log.d("------","---onDraw");
super.onDraw(canvas);
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Log.d("------","---onSizeChanged");
super.onSizeChanged(w, h, oldw, oldh);
}
}

运行结果:

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