Android Development Create reusable UI components

Source: Internet
Author: User

The Android platform provides a wide range of UI visual components---widgets that combine these small visual artifacts to provide users with a complex and useful interface. However, applications often require advanced visual components to meet such requirements, and for efficient purposes, some standard widgets can be combined into a new reusable component.

For example, it contains a progress bar and a Cancel button operation Progress representation component, a panel containing two buttons (cancel and confirm operation), a panel with an icon, title, and description, and so on. It is easy to create a UI component by writing a custom view class, but it is easier to use XML.

In the Android XML layout file, each tag corresponds to an actual class instance (this class is always a subclass of the view Class), and there are three special tags in the Android UI toolset that don't have corresponding view instances: <requestfocus/>, <merge/> and <include/>. This article shows how to use the <include/> tag to create pure XML visual components. For information about how to use <merge/>, see the "Merge Layout" article, which is associated with <include/>

Combined to use, the function will be more powerful.

As the name suggests, the <include/> tag is to include another XML layout in the current layout. The use of this tag is as simple as the example shown in the example, which refers directly to the source code of the Android home application:

<com.android.launcher.workspace

Android:id= "@+id/workspace"

Android:layout_width= "Fill_parent"

android:layout_height= "Fill_parent"

launcher:defaultscreen= "1" >

<includeandroid:id= "@+id/cell1" layout= "@layout/workspace_screen"/>

<includeandroid:id= "@+id/cell2" layout= "@layout/workspace_screen"/>

<includeandroid:id= "@+id/cell3" layout= "@layout/workspace_screen"/>

</com.android.launcher.Workspace>

Only the layout attribute is required in <include/>. The value of this property is not prefixed with the Android namespace, it is a reference to the layout file that you want to include. In the example, the same layout is included three times. The tag can also overwrite some properties that are included in the layout. The Android:id in the example above specifies the ID of the root view of the included layout, and if a new ID is defined, the ID of the included layout is overwritten. Similarly, all layout parameters can be overridden. means that any android:layout_* attribute can be used in the <include/> tag. In the following example, the same layout is included two times, but the layout property is overwritten for the first time:

<!--override the layout height and width-->

<includelayout= "@layout/image_holder"

android:layout_height= "Fill_parent"

Android:layout_width= "Fill_parent"/>

<!--does not override layout dimensions; Inherit them from Image_holder-->

<includelayout= "@layout/image_holder"/>

Warning: If you want to overwrite the size of the layout, you must overwrite both the Android:layout_height and Android:layout_width properties---you cannot cover only the height or width. If you cover only one of them, there is no effect. Properties that are not overridden will still inherit the property settings from the source layout.

This tag is especially useful when you need to customize the UI part based on the device's configuration. For example, the primary layout of an activity can be placed in the layout/directory, and then include a separate layout saved in the layout-land/and layout-port/directories, so that you can share most of the UI elements in the layout of the horizontal and vertical screens.

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.