Android layout tips for creating reusable UI components _android

Source: Internet
Author: User

The Android platform provides a large number of UI artifacts that you can build together to present a complex and useful picture of the user. However, applications sometimes require advanced visual components. To meet this requirement and to achieve it efficiently, you can combine multiple standard artifacts into a single, reusable component.

For example, you can create a reusable component that contains a progress bar and a cancellation button, a panel containing two buttons (identifying and canceling the action), and a panel containing icons, headings, and descriptions, and so on. Simply, you can create a UI component by writing a custom view, but the simpler way is to use XML only.

In the Android XML layout file, in general, each tag corresponds to a real class instance (these classes are typically subclasses of view). The UI Toolkit also allows you to use three special tags that do not correspond to specific view instances: <requestfocus/>, <merge/>, <include/>. This article will describe how to use the <include/> To create a simple XML visual component. For more information on how to use <merge/>, see the merged layout of Android layout techniques later in this article, especially the powerful power that is embodied in combining it with the <include/>.

The <include/> element acts like its name; it is used to contain other XML layouts. Use this tag as shown in the following example:

<com.android.launcher.workspace

 android:id= "@+id/workspace"

 android:layout_width= "Fill_parent"

 android:layout_height= "Fill_parent"



 launcher:defaultscreen= "1" >



 <include android:id= "@+id/cell1" layout= "@layout/workspace_screen"/> <include android:id= "@+id/cell2" layout= "

 @layout/workspace_screen" />

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



</ Com.android.launcher.workspace>

In <include/>, only the layout feature is required. This feature, without the Android namespace prefix, represents a reference to the layout you want to include. In this example, the same layout is included three times. This tag also allows you to rewrite some of the features that are included in the layout. The above example shows that you can use Android:id to specify the ID of the root view in the included layout, and it can override the already defined layout ID. Similarly, you can rewrite all the layout parameters. This means that any android:layout_* feature can be used in <include/>. Here is an example:

<include android:layout_width= "fill_parent" layout= "@layout/image_holder"/> <include

_width= "256dip" layout= "@layout/image_holder"/>

This tab is especially useful when customizing the UI against the device. For example, the main layout of the activity is placed under the layout/folder, and other layouts are placed under layout-land/and layout-port/. This allows you to share most of the UI layouts in both vertical and horizontal directions.

include tags can be used to refer to the layout of another layout in one layout, which is usually suitable for apps with complex interface layouts, shared layouts for different interfaces, such as the top layout of an app, the layout of sidebar, the bottom tab bar layout, ListView and GridView each item layout, and so on, the same app has a number of interfaces used in the layout extracted and then through the include tag reference, can reduce the complexity of the layout, but also to achieve layout reuse (layout changes only need to modify a place on it).

How to use include tags :
the include tag is easy to use, just need to refer to other layouts in the layout file, use the layout= "@layout/child_layout" is OK:
<include layout= "@layout/titlebar"/>

about include tags related content in the future articles will be for everyone to share, the above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud-dwelling community.

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.