Use the Include tag and merge tab to reuse layouts in Android _android

Source: Internet
Author: User

Although Android offers a variety of components for small, reusable interaction elements, you may also need to reuse a large component for the layout. To efficiently reuse the full layout, you can use the <include/> and <merge/> tags to embed another layout into the current layout. So when you create a standalone UI component by writing a custom view, you can put it in a layout file, which is easier to reuse.

The reuse layout is very powerful because it allows you to create complex layouts that can be reused. For example, a Yes/no button panel, or a custom progress bar with descriptive text. This also means that common elements in multiple layouts in an application can be extracted, managed independently, and inserted into each layout.

Create a reusable layout

If you already know which layout needs to be reused, create a new XML file to define the layout. For example, here is a layout that defines the title bar from the G-kenya code base, which can be inserted into each activity:

Copy Code code as follows:

<framelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:background= "@color/TITLEBAR_BG" >

<imageview android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:src= "@drawable/gafricalogo"/>
</FrameLayout>

The root view should be exactly where you are in the other views that you want to insert into this view.

Use <include/> label

Add <include/> tag in the layout where you want to add a reusable layout. The following is the Add title bar:

Copy Code code as follows:

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:background= "@color/APP_BG"
android:gravity= "Center_horizontal" >

<include layout= "@layout/titlebar"/>

<textview android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/hello"
android:padding= "10DP"/>

...

</LinearLayout>

You can also overwrite all layout parameters (Android:layout_* properties)

Copy Code code as follows:

<include android:id= "@+id/news_title"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
layout= "@layout/title"/>

However, if you want to overwrite layout attributes with include tags, you must overwrite Android:layout_height and android:layout_width for other attributes to take effect.

Use <merge/> label

<merge/> tags help you eliminate unwanted view when inserting a layout into another layout Group. For example, your multiplexed layout is a vertical linear layout that contains two child views, and when it is inserted into another vertical linear layout as a reused element, the result is a vertical linearlayout containing a vertical linearlayout. This nesting layout has no practical meaning and can make UI performance worse.

To avoid inserting a similar redundant view Group, you can use the <merge/> label label as the root node for reusable layouts, such as:

Copy Code code as follows:

<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >

<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/add"/>

<button
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "@string/delete"/>

</merge>

Now, when you use the Include tag to insert this layout into another layout, the system ignores the merge label and replaces the two button directly with the Include label location.

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.