Android Development Guide-framework topic-User Interface

Source: Internet
Author: User
Document directory
  • Adapter

User InterfaceUser Interface

Key
  1. View
  2. Viewgroup
  3. Widget classes
In an Android Application, the user interface is built by the view and viewgroup objects. Both view and viewgroup have many types, and they are subclasses of view classes. The view object is the basic unit embodied in the user interface of the Android platform. The View class is the basis of its subclass called "widgets (Tool)". It provides complete implementation of UI objects such as text input boxes and buttons. The viewgroup class also lays the foundation for its subclass called layouts (layout). They provide layout architectures such as stream layout, table layout, and relative layout. A view object is a data body that stores layout parameters and content for a rectangular area on the screen. It is responsible for all the measurement, layout, focus conversion, rolling and processing of the button/touch gesture in the rectangle area under its jurisdiction. As a user interface object, view also serves as the key point of user interaction and the receiver of Interaction Events. View hierarchy

On the Android platform, you can define the UI of an activity using the view and viewgroup hierarchies shown in. This hierarchy tree can be as simple or complex as you wish. You can use a predefined set of Android tools and layout to create it, or use your own defined views to create it.

To display a view hierarchy to the screen, your activity must callsetContentView()And pass it a reference to the Root Node object. The Android system will accept this reference and use it to abolish, measure, and draw this tree. The root node of a hierarchy requires its child nodes to draw itself-and each view group node is also responsible for calling its child views for self-drawing. The child node applies to the parent node for the position and size of the painting, and its parent class has the final decision on the position and size of the child node. Android parses the elements in your layout in sequence (starting from the top layer of the hierarchy tree), instantiates views, and adds them to their parent nodes. Because this process is performed sequentially, if the elements overlap, the last element to be drawn will appear above all the overlapping elements. For more details about how to calculate and draw a view level, see how to draw a view in Android. Layout

The most common way to define and display your view hierarchy is to use an XML layout file. Like HTML, XML provides a readable structure for the layout. Each element in XML is a view or viewgroup object (or their subclass ). The view object is the leaf node of the tree, and the viewgroup object is the branch of the tree (see the view hierarchy chart on the upper floor ).

The name of the XML element corresponds to the Java class it represents. So one<Textview> an element is generated in your UI.Textview, and<Linearlayout> createsLinearlayout view group. When you load a layout resource, the android system initializes these runtime objects based on the elements in your layout.

For example, a simple vertical layout that contains a text view and a button is as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>

Note: The linearlayout element contains textview and button objects. You can place another linearlayout (or another type of view group) in it to extend this view level and build a more complex layout.

For more information about how to build a UI layout, see declare a layout.

Tip:You can also use Java code to draw view and viewgroup objects andThe addview (View) method dynamically inserts a new view and viewgroup object.

You have many ways to layout a view. Using a large number of different view groups, you can build subviews and view groups in almost infinite ways. Android provides some predefined view groups, including linearlayout, relativelayout, absolutelayout, tablelayout, gridlayout, and others. Each provides a unique set of layout parameters for defining sub-views and layout structures.

For details about other view groups used for layout, see common layout objects.

Widget Widgets

A widget is a view object that provides services for the user interaction interface. Android provides a complete set of components, including buttons, check boxes, and text input boxes, to help you quickly build the UI. Android also provides more advanced components, such as date selection, clock, and scaling control. However, you are not limited to the components provided by the Android platform. If you want to create custom action elements, you only need to define your own view object or expand or merge existing parts.

For more information, see build custom components.

You can find the parts list provided by Android in the Android. widget package.

User Interface event UI events

After you add some views and tools to the user interface, you may want to know how to make them interact with the user to implement your actions. To receive user interface Event Notifications, you need to do one of the following two things:

  • Define an event listener and register it to the view.In general, this is the main way you listen for events. The View class contains a lot of names similar to on<What>Listener interface, each withOn<What>() Callback method. For example:View. onclicklistener (used to process clicks in the view), view. ontouchlistener (used to process touch screen events in the view), and view. onkeylistener (used to process device button events in the view ). Therefore, if you want your view to be notified when it is clicked (for example, when a button is selected), you need to implement onclicklistener and define itsonClick()Callback method (in which the corresponding processing is performed), and usesetOnClickListener()Method is registered to the view.
  • Overwrite an existing callback method for the view.This method is mainly used to implement a View class and listen to specific events on it. For example, when a screen is touched (onTouchEvent()When the trackball moves (onTrackballEvent()) Or the device's buttons are pressed (onkeydown ()). This method allows you to define the default behavior for each event in your custom view and decide whether to pass the event to other subviews. Again, these are view-related callback methods, so you can only define them when you build custom components.
For how to process user interaction in the view, see the document for handling user interface events. Menu menus

The Application menu is another important component of the application user interface. Menus provide a reliable interface for displaying application functions and settings. Press the menu key on the device to bring up the most common application menu. However, you can also add context menus that are called up when you long press a project.

Menus are structured by view layers, but you do not have to define the architecture yourself. You just need to define your activityonCreateOptionsMenu()AndonCreateContextMenu()Callback method, and declare the project you want to include in the menu. Android will automatically create a view level for your menu and draw your menu items in it.

Menus process their events by themselves, so you do not have to register an event listener for your project. When an item in your menu is selected, the framework will automatically callonOptionsItemSelected()OronContextItemSelected()Method.

Just like the application layout. You can also define projects in your menu in an XML file.

For more information, see create a menu.

Advanced topics

Once you know the basics of creating user interfaces, you can try to use some advanced functions to create more complex application interfaces.

Adapter

Sometimes you want to fill the view group with information that cannot be hard-coded. You want to bind external data to your view. To achieve this goal, you can use adapterview as your view group, initialize each subview with the data sent from the adapter, and fill in it.

An adapterview object is a viewgroup that constructs its subviews based on a given adapter object. The adapter plays a messenger role between your data source (which may be an external String Array) and the adapterview that displays the data. There are many different adapter class implementations for specific tasks. For example, cursoradapter reads data from a database based on cursor, while an arrayadapter reads data from any array.

To learn how to use an adapter to populate your view, see bind an adapterview to Data.

Styles and themes

Maybe you are not so satisfied with the appearance of the standard tool. To solve this problem, you can create your own style and topic.

  • Style is a set of elements that contain one or more formatting attributes. You can add them to a single element in your layout. For example, you can define a style that contains the font size and color of a specific text and apply it to a specific view element separately.
  • A topic is also a set of attributes that contain one or more formatting attributes, but is applied to all the activities in an application, or a single activity. For example, you can define a theme that contains the border color and layout background of a specific window, as well as a set of font sizes and menu colors. This topic can be applied to a specific activity or the entire application.
Style and topic belong to resources. Android provides some default styles and themes for you to use. You can also customize your own styles and theme resources. For more information about the style and topic, see the style and topic document.

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.