View绘制时, 方法执行顺序

清晨宸 2015-11-16 11:28:42
自定义控件MyView

public class MyView extends View {
private static int index=0;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
log("create MyView");
}
private void log(String msg) {
Log.d("chen","index="+(index++)+"- -"+msg);
}


@Override
public void requestLayout() {
super.requestLayout();
log("requestLayout");
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
log("onMeasure");
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
log("onLayout");
}
}


打印的Log:
index=0- -requestLayout
index=1- -create MyView
index=2- -requestLayout
index=3- -onMeasure
index=4- -onLayout

问题:
为什么requestLayout可以在create之前被调用??还没有通过构造方法创建对象,怎么能调用requestLayout啊?
...全文
135 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ComputerBlue 2015-11-19
  • 打赏
  • 举报
回复
我试了下, 是构造方法先执行的,不是requestLayout()。。。。
=10_01= 2015-11-17
  • 打赏
  • 举报
回复
http://developer.android.com/reference/android/view/View.html http://www.android-doc.com/reference/android/view/View.html onFinishInflate() > onMeasure(int, int) > onLayout(boolean, int, int, int, int) > onSizeChanged(int, int, int, int) > onDraw(android.graphics.Canvas) > onKeyDown(int, KeyEvent) > onKeyUp(int, KeyEvent) > onTrackballEvent(MotionEvent) > onTouchEvent(MotionEvent) > onFocusChanged(boolean, int, android.graphics.Rect) > onWindowFocusChanged(boolean) > onAttachedToWindow() > onDetachedFromWindow() > onWindowVisibilityChanged(int)

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧