[Excerpt] Android screen element hierarchy

Source: Internet
Author: User

Android screen element hierarchy

Android. App. Activity
For an Android app, an android. App. Activity instance is the most basic function unit. An activity instance can do a lot of things, but it cannot be displayed on the screen. Instead, viewgroup and view are used. These two are the two most basic user interface expression units on the Android platform.

Android. View. viewgroup
Viewgroup is a special view class that inherits from Android. View. View. Its function is to load and manage the view objects and viewgroup objects at the next layer. Viewgroup is the base class of the layout manager (layout) and view container.
Viewgroup also defines a nested class viewgroup. layoutparams. This class defines the location, size, and other attributes of the display object. View uses these attribute values in layoutparams to tell the parent level how to place them.
Here, some main layout classes inherited from viewgroup are as follows:
1. framelayout: the simplest layout object. It only displays one display object. All Display objects are fixed in the upper left corner of the screen and cannot be specified. However, multiple explicit objects are allowed, but the last one will overwrite the previous one and block the previous one or all (unless the last one is transparent ).
2. linearlayout: arrange and display the display objects in a single direction. For example, if the display objects are vertically arranged, the layout manager only has one column. For example, the display objects are horizontally arranged, there will be only one row in the layout manager. It can also set the display ratio for individual display objects.
3. tablelayout: la s display objects in tables with any row or column. Each Display object is allocated to its respective cells, but its border line is invisible.
4. absolutelayout: allows you to specify the specific position of the object to be displayed in the form of coordinates. The coordinates in the upper left corner are (0, 0), down and right, and the coordinates increase. Because the location of the display object is fixed, the layout manager may have different display effects on different devices.
5. relativelayout: Allows layout by specifying the relative position of the display object relative to other display objects or parent objects. For example, a button can be placed on the right of another button or in the center of the layout manager.
In Android, many layout managers are provided, which are not listed here. developers can select the appropriate layout manager based on their actual needs.

Android. View. View

View is the base class of all view classes. A view usually occupies a rectangular area on the screen and is responsible for drawing and event processing. View is the base class of all form parts and serves form parts. The form part is a UI control, such as a button or text box. Android has provided us with a series of standard UI controls for our direct use. At the same time, we can also implement our custom UI controls by inheriting to the View class or view subclass.
To customize our own UI controls, you need to reload some methods in the View class. The following table lists the methods provided by the view for overloading. These methods do not have to be reloaded, but at least ondraw (Android. graphics. canvas) method.

Category Method Description
Creation Constructors  
Onfinishinflate () This method is called when view and all its sub-objects are imported from XML.
Layout Onmeasure (INT, INT) View will call this method to confirm the size of itself and all sub-objects
Onlayout (Boolean, Int, INT) This method is called when the view needs to allocate the size and position of all sub-objects.
Onsizechanged (INT, Int, Int, INT) This method is called when the View Size changes.
Drawing Ondraw (canvas) This method is called when a view wants to draw its content
Event Processing Onkeydown (INT, keyevent) This method is called when a new key event occurs.
Onkeyup (INT, keyevent) This method is called when a key release event occurs.
Onmotionevent (motionevent) This method is called when an action event (such as touch) occurs.
Focus Onfocuschanged (Boolean, INT) This method is called when the view gets or loses focus.
Attaching Onattachedtowindow () This method is called when a view is attached to a form.
Ondetachedfromwindow () This method is called when a view leaves its form.

When you add a visible view to an activty and run this activty, Android will automatically trigger view-related events in the following order.

Onattachedtowindow
Onmeasure
Onsizechanged
Onlayout
Ondraw

For a screen in an Android app, its screen elements are described in a hierarchical structure. To bind a screen element hierarchy tree to a screen for display, activity will call its setcontentview () method and pass in the root node reference of this hierarchy tree. When the activity is activated and the focus is obtained, the system will notify the activity and request the root node to calculate and draw the tree. The root node will request its child nodes to draw their own.
The viewgroup node on each tree is used to draw its child nodes. Viewgroup calculates its effective space, layout all sub-display objects, and finally calls the draw () method of all sub-display objects to draw the display objects. Each child Display object can request the size and position of the parent object in the layout, but the parent object determines the size and position of each child Display object.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.