View
官方说明:这个类是用户接口的基础构件。View表示屏幕上的一块矩形区域,负责绘制这个区域和事件处理。
View是所有widget类的基类,Widget类用于创建交互式UI构件(按钮,输入框等)。
View类的ViewGroup子类是layouts的基类,Layout是一个不可见的容器,它容纳着其他View或ViewGroup并定义这些View的layout 属性。
View要点
- view.setOnTouchListener()注册的监听要先于View中默认的onTouchEvent()方法执行,即:onTouch()要先于onTouchEvent()执行,否则无法达到覆盖的Touch事件的目的;
- onClick()在onTouchEvent()中的ACTION_UP之后才会回调执行,关于onLongClick(),在ACTION_DOWN之后启动一个定时器180ms,时间计时完成之后就会回调onLongClick(),所以onLongClick()是在ACTION_UP之前执行的;
- EditText注册onClick事件之后会导致系统键盘中的“下一项”功能失效;
自定义View通常可实现的方法
Creation
-
Constructors 构造方法
-
onFinishInflate()
Called after a view and all of its children has been inflated from XML.
一般用于自定义ViewGroup中,当此ViewGroup及其所有子View从XML中inflate完成后,就会回调此方法。
另外,重写此方法时,要确保super.onFinishInflate()的执行。
Layout
-
onMeasure(int, int)
Called to determine the size requirements for this view and all of its children.
用于确定View及其子View的大小区域
-
onLayout(boolean, int, int, int, int)
Called when this view should assign a size and position to all of its children.
用于分配子View大小与位置的方法
-
onSizeChanged(int, int, int, int)
Called when the size of this view has changed.
当View的大小有变化时回调此方法
Drawing
-
onDraw(android.graphics.Canvas)
Called when the view should render its content.
将View渲染绘制到屏幕上的方法
Event processing
-
onKeyDown(int, KeyEvent)
Called when a new hardware key event occurs.
物理按键按下的回调
-
onKeyUp(int, KeyEvent)
Called when a hardware key up event occurs.
物理按键弹起的回调
-
onTrackballEvent(MotionEvent)
Called when a trackball motion event occurs.
监听轨迹球事件的回调(==现在开发中基本已弃用==,用触摸事件基本可以满足所需功能,轨迹球初始大多用于一些游戏的开发中)
-
onTouchEvent(MotionEvent)
Called when a touch screen motion event occurs.
触摸屏幕的回调
Focus
-
onFocusChanged(boolean, int, android.graphics.Rect)
Called when the view gains or loses focus.
View获得或者失去焦点时回调
-
onWindowFocusChanged(boolean)
Called when the window containing the view gains or loses focus.
包含此View的Window获得or失去焦点时回调此方法
Attaching
-
onAttachToWindow()
Called when the View attached to a window.
当View被添加到一个Window中时回调此方法
-
onDetachedFromWindow()
Called when the View is dettached from its window
当View从Window中被移除时回调此方法
-
onWindowVisibilityChanged(int)
Called when the window containing the view has changed
当此View的Window的可见性发生变化时回调此方法。
方法
onFinishInflate
Called after a view and all of its children has been inflated from XML.
当此View及其所有的子View,从XMl中被inflate出来之后,此View的onFinishinflate会被回调。
当自定义View并重写此方法时,确保super.onFinishInflate()的执行
bringToFront
ViewGroup#bringChildToFront(View)
Change the z order of the child so it's on top of all other children. This ordering change may affect layout, if this container uses an order-dependent layout scheme (e.g., LinearLayout). Prior to KITKAT this method should be followed by calls to requestLayout() and invalidate() on this parent to force the parent to redraw with the new child ordering.
将当前View的顺序调到父容器的最前面。当父容器是LinearLayout等具有顺序依赖的容器时,可能会影响其布局展示效果。另外,当API 19之前的版本调用此方法时,需要在其后紧接着调用其parent的requestLayout()与invalidate()方法来重绘布局。
网友评论