码迷,mamicode.com
首页 > 其他好文 > 详细

自定义View 中一些方法的调用时机

时间:2015-01-20 17:59:06      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:自定义view

onFinishInflate()函数的调用时机:


onFinishInflate() 当View中所有的子控件均被映射成xml后触发


onMeasure(int, int) 确定所有子元素的大小


onLayout(boolean, int, int, int, int) 当View分配所有的子元素的大小和位置时触发


onSizeChanged(int, int, int, int) 当view的大小发生变化时触发


onDraw(Canvas) view渲染内容的细节


onKeyDown(int, KeyEvent) 有按键按下后触发


onKeyUp(int, KeyEvent) 有按键按下后弹起时触发


onTrackballEvent(MotionEvent) 轨迹球事件


onTouchEvent(MotionEvent) 触屏事件


onFocusChanged(boolean, int, Rect) 当View获取或失去焦点时触发 


onWindowFocusChanged(boolean) 当窗口包含的view获取或失去焦点时触发


onAttachedToWindow() 当view被附着到一个窗口时触发


onDetachedFromWindow() 当view离开附着的窗口时触发,该方法和  onAttachedToWindow() 是相反。


onWindowVisibilityChanged(int) 当窗口中包含的可见的view发生变化时触发



另外,今天写了一个自定义View 的demo, 然后再布局文件中 使用完整包名 引用,但是一运行程序就报错,提示

Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.qiquan.demo_customview.view.CustomView

经查询,原因是自定义View的类中需要重写三个构造方法,而我只重写了一个,

/**

         *代码创建时调用
* Simple constructor to use when creating a view from code
* @param context
*/
public CustomView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

/**

         *布局文件加载时调用
* Constructor that is called when inflating a view from XML
* @param context
* @param attrs
*/
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub


}


/**
* Perform inflation from XML and apply a class-specific base style
* @param context
* @param attrs
* @param defStyle
*/
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}

















自定义View 中一些方法的调用时机

标签:自定义view

原文地址:http://blog.csdn.net/daiqiquan/article/details/42920959

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!