View之方法记录

2019-12-17  本文已影响0人  钦_79f7

View

官方说明:这个类是用户接口的基础构件。View表示屏幕上的一块矩形区域,负责绘制这个区域和事件处理。
View是所有widget类的基类,Widget类用于创建交互式UI构件(按钮,输入框等)。
View类的ViewGroup子类是layouts的基类,Layout是一个不可见的容器,它容纳着其他View或ViewGroup并定义这些View的layout 属性。

View要点

  1. view.setOnTouchListener()注册的监听要先于View中默认的onTouchEvent()方法执行,即:onTouch()要先于onTouchEvent()执行,否则无法达到覆盖的Touch事件的目的;
  2. onClick()在onTouchEvent()中的ACTION_UP之后才会回调执行,关于onLongClick(),在ACTION_DOWN之后启动一个定时器180ms,时间计时完成之后就会回调onLongClick(),所以onLongClick()是在ACTION_UP之前执行的;
  3. EditText注册onClick事件之后会导致系统键盘中的“下一项”功能失效;

自定义View通常可实现的方法

Creation

Layout

Drawing

Event processing

Focus

Attaching

方法

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()方法来重绘布局。

相关资料

上一篇 下一篇

猜你喜欢

热点阅读