Use tags to reuse layout code

Source: Internet
Author: User
Http://www.chengyunfeng.com/2010/01/android-layout-optimization-of-the-two-use-include-and-merge
If you need to use the same layout design in a project, you can use the <include/> label to reuse the layout code. This label is not described in the android development documentation. This tag is used in the android main screen program:

<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>

You can reference a layout multiple times without copying or pasting it repeatedly. You can use the include tag to override the values of some attributes. For example, the preceding example overwrites the id value in the referenced layout. The following is another example:

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

Use the <merge/> label to reduce the view hierarchy

In the Android layout file, a top-level container is required to accommodate other components, but multiple components cannot be placed directly. For example, the following code
:

<FrameLayout xmlns: android = "http://schemas.android.com/apk/res/android"

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent">

<ImageView

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: scaleType = "center"

Android: src = "@ drawable/golden_gate"/>

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "Golden Gate"/>

</FrameLayout>


The code above shows an image and a title on the top of the image. The result is as follows:

The default layout of the android activity is FrameLayout, so that the layout code above has two layers of FrameLayout, through HierarchyViewer
The structure of the tool is as follows:

If you can remove the FrameLayout statement from the layout file, you can further optimize the layout code. However, because the layout code needs to be accommodated by the outer container, if
If you delete FrameLayout directly, this file is not a legal layout file. In this case, you can use the <merge/> label.
Modify the Code as follows to eliminate unnecessary FrameLayout:

<Merge xmlns: android = "http://schemas.android.com/apk/res/android">

<ImageView

Android: layout_width = "fill_parent"

Android: layout_height = "fill_parent"

Android: scaleType = "center"

Android: src = "@ drawable/golden_gate"/>

<TextView

Android: layout_width = "wrap_content"

Android: layout_height = "wrap_content"

Android: text = "Golden Gate"/>

</Merge>

The HierarchyViewer tool shows the following structure:

Merge also has some restrictions: it can only be used for the root element of the xml layout file; inflate a merge-based root element in the code
When layout a file, you must specify a ViewGroup as its container and set attachToRoot to true. For more information, see
The inflate () function doc.

Sample project code above:
Http://progx.org/users/Gfx/android/MergeLayout.zip

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.