Android view lifecycle

What is View?

View class represents thebasic building block for user interface components.A View occupies a rectangular area on the screen and isresponsible for drawing and event handling.View is the base class for widgets, which are used to create interactive UI components (buttons, text fields, etc.). TheViewGroup subclass is the base class for layouts, which are invisible containers that hold other Views (or other ViewGroups) and define their layout properties.

Using Views

All of the views in a window are arranged in a single tree. You can add views either from code or by specifying a tree of views in one or more XML layout files. There are many specialized subclasses of views that act as controls or are capable of displaying text, images, or other content. Once you have created a tree of views, there are typically a few types of common operations you may wish to perform:
  • Set properties: for example setting the text of a TextView. The available properties and the methods that set them will vary among the different subclasses of views. Note that properties that are known at build time can be set in the XML layout files.
  • Set focus: The framework will handled moving focus in response to user input. To force focus to a specific view, callrequestFocus().
  • Set up listeners: Views allow clients to set listeners that will be notified when something interesting happens to the view. For example, all views will let you set a listener to be notified when the view gains or loses focus. You can register such a listener using setOnFocusChangeListener(android.view.View.OnFocusChangeListener). Other view subclasses offer more specialized listeners. For example, a Button exposes a listener to notify clients when the button is clicked.
  • Set visibility: You can hide or show views using setVisibility(int).

Note:The Android framework is responsible for measuring, laying out and drawing views. You should not call methods that perform these actions on views yourself unless you are actually implementing a ViewGroup.

Implementing a Custom View

To implement a custom view, you will usually begin by providing overrides for some of the standard methods that the framework calls on all views. You do not need to override all of these methods. In fact, you can start by just overriding onDraw(android.graphics.Canvas). Category Methods Description
Creation Constructors There is a form of the constructor that arecalled when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file.
onFinishInflate() Called after a view and all of its children has been inflated from XML.
Layout onMeasure(int, int) Called to determine the size requirementsfor this view and all of its children.
onLayout(boolean, int, int, int, int) Called when this view should assign a size and position to all of its children.
onSizeChanged(int, int, int, int) Called when the size of this view has changed.
Drawing onDraw(android.graphics.Canvas) Called when the view should render its content.
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.
onWindowFocusChanged(boolean) Called when the window containing the view gains or loses focus.
Attaching onAttachedToWindow() Called when the view is attached to a window.
onDetachedFromWindow() Called when the view is detached from its window.
onWindowVisibilityChanged(int) Called when the visibility of the window containing the view has changed.

View lifecycle


出处:http://ndquangr.blogspot.com/2013/04/android-view-lifecycle.html

更多相关文章

  1. Android-ksoap2与NuSOAP的交互(原创)
  2. Android(安卓)防止Toast重复弹出相同的信息
  3. Android(安卓)Layout Tricks #2: Reusing layouts
  4. 网页中android Toast效果的实现
  5. Android(安卓)软件自动更新功能实现的方法
  6. [置顶] Android下的stl读取3D显示
  7. android 自定义 3D View
  8. Fragment保存数据
  9. [置顶] 电源管理之android内核suspend to disk的实现(六)--STD执行

随机推荐

  1. 选择器权重及伪类选择器练习
  2. 简单的登陆表单、后台框架、元素样式来源
  3. css选择器权重、伪类选择器计算方式
  4. HTML入门之常用伪类选择器的使用方法总结
  5. laravel数据库查询结果自动转数组修改实
  6. 选择器知识汇总
  7. PHP并发场景的三种解决方案代码实例
  8. 常用伪类选择器的实例演示及参数
  9. Android中ActionBar及Overflow的显示
  10. Android(安卓)Relative Layout 安卓相对