Android layout tricks #2: reusing layouts (Android layout skills 2: Reuse layout)

Source: Internet
Author: User
25 February 2009

Android layout tricks #2: reusing layouts

Android comes with a wide varietyWidgets, Small Visual Construction blocks you can glue together to present the users with complex and useful interfaces. However applications often need higher level visual
Components. A component can be seen as a complex widget made of several simple stock widgets. you cocould for instance reuse a panel containing a progress bar and a Cancel button, a panel containing two buttons (positive and negative actions), a panel
With an icon, a title and a description, etc. Creating new components can be done easily by writing a custom
ViewBut it can be done even more easily using only XML.

In Android XML layout files, each tag is mapped to an actual class instance (the class is always a subclass
View.) The UI toolkit lets you also use three special tags that are not mapped to
ViewInstance:<requestFocus />,<merge />And
<include />. The latter,<include />, Can be used to create pure XML visual components. (Note: I will present
<merge />Tag in the next installmentAndroid layout tricks.)

The<include />Does exactly what its name suggests; it has des another XML layout. Using this tag is straightforward as shown in the following example, taken straight from

The source code of the home application that currently ships with Android:

<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 />OnlylayoutAttribute is required. This attribute, without
androidNamespace prefix, is a reference to the layout file you wish to include. in this example, the same layout is wrongly ded three times in a row. this tag also lets you override a few attributes of the specified ded layout. the above example shows
That you can useandroid:idTo specify the ID of the Root View of the specified ded layout; it will also override the ID of the specified ded layout if one is defined. similarly, you can override all the layout parameters. this means that any
android:layout_*Attribute can be used with<include />Tag. Here is an example:

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

This tag is special useful when you need to customize only part of your UI depending on the device's configuration. For instance, the main layout of your activity can be placed in
layout/Directory and can include another layout which exists in two flavors, in
layout-land/Andlayout-port/. This allows you to share most of the UI in portrait and landscape.

Like I mentioned earlier, my next post will explain<merge />, Which can be maid powerful when combined
<include />.

Posted byromain guyat10: 31
PMLabels: How-to, optimization, user
Interface

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.