android view 中各函数的执行顺数

这个就好像是 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 }
复制代码

 

运行结果:

1
2
3
4
5
6
7
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

  

posted @   指尖下的幽灵  阅读(914)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示