Android screen element hierarchy

Source: Internet
Author: User
For an Android mobile app, a nice and concise interface is very important, which not only improves the user experience, but also ensures the efficient execution of the app. The android SDK has provided us with a complete set of interface design functions, with a wide range of components for our use. If we still cannot meet our interface requirements, we can also expand them. To achieve this goal, we need to thoroughly learn the Interface Generation Technology provided in the android SDK, so that the application can be freely implemented.
This article, referring to the relevant documentation provided by Android, only summarizes the layered structure of the screen elements of Android and has a general grasp of the android interface design. I think this is necessary.

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 display objects are allowed, but the last one will overwrite the previous one to block the previous one (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, int, int, 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.

The above events are automatically handled by Android. For other important events, such as mouse clicks, We must manually add them to the view. This is depressing, the specific method to add is as follows:

public class SendResult extends Activity
{
protected void onCreate(Bundle savedValues)
{
...

Button button = (Button)findViewById(R.id.corky);
button.setOnClickListener(mCorkyListener);
}

private OnClickListener mCorkyListener = new OnClickListener()
{
public void onClick(View v)
{
setResult(RESULT_OK, "Corky!");
finish();
}
};

Screen element hierarchyFor 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.