美文网首页
自定义View之方法清单

自定义View之方法清单

作者: 钦_79f7 | 来源:发表于2019-12-17 12:33 被阅读0次

    自定义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的大小区域

      关于测量模式:

      • UNSPECIFIED:父容器没有对当前View有任何限制,当前View可以任意取尺寸

      • EXACTLY:当前的尺寸就是当前View应该取的尺寸

      • AT_MOST:当前尺寸是当前View能取的最大尺寸

        wrap_content -> AT_MOST

        match_parent、固定尺寸 -> EXACTLY

    • 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的可见性发生变化时回调此方法。

    关键点

    • 自定义View中获取自定义属性是要采用Context中方法,不要采用getResource中的方法,否则自定义属性定义在style中在真机中是无法获取到style中设置的属性。

    相关文章

      网友评论

          本文标题:自定义View之方法清单

          本文链接:https://www.haomeiwen.com/subject/mlssnctx.html