User Interface In an Android Application, user interfaces are combined by a series of view and viewgroup objects. Android has many types of view and viewgroup objects, all of which inherit from the view base class. The view object is the basic object of the android user interface. These view classes act as the parent class of the widgets widget that interacts with users, such as text boxes and buttons. Viewgroup is the parent class of layouts that provides various layout structures, such as linear layout, table layout, and absolute layout. A view object is a data structure that stores layout parameters and the content of the rectangular area in a specific area of the screen. A view processes the measurement, layout, painting, focus change, scrolling, and interaction with the key gesture in the area of its screen. As a user interaction object, a view can be used as an interactive tool between users and the system to receive events. View Architecture On the Android platform, you need to use the view or viewgroup layer and node to define an android user interface, just like the following chart, this hierarchy tree can be simple or complex as you need it. You can use a widget, layout, or custom layout that has been defined in the Android system.
To render your view hierarchy on the screen, your activity needs to call the setcontentview () method and pass a reference to the Root Node object. The Android system receives these configurations and uses them to measure and draw the tree view structure. The root node of this view structure requires the child node to be self-drawn. Conversely, each viewgroup node is responsible for self-painting of their child nodes. The size and position of the child node are requested from the parent node, but the parent object determines the size of the child. Android parses all the elements in your layout in sequence, instantiates views from the top, and adds them to the parent object. Because they are all drawn in order, if some views exceed the display range, the previously drawn view will overwrite the original one. How to draw the view structure system will be discussed in detail later. Layout The most common layout is defined in XML. XML provides an easy-to-read structure, much like HTML. Each element in XML is a view or viewgroup object (or their subclass ). The view is a leaf node in the tree structure, and the viewgroup object is on a non-leaf node (For details, refer ). The XML element name represents each class. For example, the <textview> element will create a textview control, and a <linearlayout> will create a linearlayout viewgroup. When you load a layout resource, the android system will initialize the runtime object, that is, the corresponding layout element. For example, a simple vertical layout contains a textview and a button.
1 <? XML version = "1.0" encoding = "UTF-8"?> 2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" 3 Android: layout_width = "fill_parent" 4 Android: layout_height = "fill_parent" 5 Android: Orientation = "vertical"> 6 <textview Android: Id = "@ + ID/text" 7 Android: layout_width = "wrap_content" 8 Android: layout_height = "wrap_content" 9 Android: text = "Hello, I am a textview"/> 10 <button Android: Id = "@ + ID/button" 11 Android: layout_width = "wrap_content" 12 Android: layout_height = "wrap_content" 13 Android: text = "Hello, I am a button"/> 14 </linearlayout> Copy code Note that the linearlayout element contains textview and button. You can nest another linearlayout to increase the view length or add a more complex layout. For more information about UI layout, see declaring layout. You can use many la s in your layout. using many different viewgroups, you can define an infinite number of sub-views or sub-viewgroups. Android provides some predefined layout s, including: linearlayout-linear layout, relativelayout-relational layout, tablelayout-table layout, and gridlayout. They provide some unique parameters to define the view location and layout structure. Widgets A widget is a view object that serves user-interface interaction. Android provides many view widgets, such as buttons, scammers, text input boxes, and so on. You can quickly build your own layout. Some widgets are very complex, such as the date selector, a clock, scaling control. However, you will not be restricted by the UI components provided by the Android platform. You can customize the UI components, inherit the existing ones, or combine them with existing ones. UI events Once you add a view or widget to the interface, you may think about how they interact with users so that you can perform some operations. To add a UI event, you need to do two things: @ Define the event listener and register it with the view. In most cases, this is how you listen for events: the view class contains some listeners named onxxxlistener, all of which are called onxxx () callback methods. For example, view. onclicklistener (which processes click events), view. ontouchlistener (processing touch events), View . Onkeylistener (handle button events), so if you want the view to respond to click events, for example, if the button is selected, You need to implement the onclicklistener interface and define the callback method, and use setonclicklistener () to register the view. @ Override an existing callback method: If you lose your view class and want to listen to some special events, you should use this method. For example, you can handle screen touch events, scroll ball events, buttons, and other events. It allows you to define the default event, that is, each event of the custom view, to determine whether the time will be passed to some word view. Then, they call the callback function of the View class, so you have the opportunity to use this method only when you customize a component. Menu The Application menu is another important part of the UI. Menu provides some reliable excuses to demonstrate program functions and some settings. Generally, the menu is displayed by pressing the menu key. However, you can ask the user to press or hold down a certain item to display the menu. Menu menus also follow the view hierarchy, but do not define them by yourself. Instead, you only need to define the oncreateoptionsmenu () and oncreatecontextmenu () callback methods for your activity. At the appropriate time, android automatically draws the necessary view structure and the sub-item of the menu. The menu will process his own time. Therefore, you do not need to register the event listener. When an item in the menu is selected, the system will call the onoptionsitemselected () method or the oncontextitemselected () method. Similar to the application layout, you can use XML to configure your menu items. Advanced features Once you understand the basic principles for creating a user interface. You can browse some advanced features to create more complex application interfaces. Adapter adapters Sometimes, you don't want to use "hard code" to fill in some view data. On the contrary, you want to bind the view to an additional dataset. In this case, you need to define an adapterview. The data in each sub-view is filled by the adapter. Adapterview is an implemented viewgroup interface, and its subclass is determined by the given adapter object. The adapter is like the adapter between your adapter view and the data source. There are several implementation methods of the adapter class, for special tasks, such as a custom adapter to read database data from a cursor, or an array adapter to read data from any array. Styles and themes You may not be satisfied with the appearance of standard controls. You can customize their style and themes to change them. @ A style is a formatted attribute set, which you use as a unit of layout. For example, you can define the text size and color of some texts as special view elements. @ A theme topic is a formatted property set of the entire activity in the application. For example, you can define the border of the form and the background of the Panel, and set the font size and color of the menu. It can be applied in the entire program. Style and topic are resources. Android provides some default style and theme resources for your use. Or you can customize them. |