Use of merge and include tags in Android

Source: Internet
Author: User
For how to use merge and include tags in Android, see the android document layout tricks: merging.

First, let's talk about the first simple usage of the <merge/> label. If we use framelayout as the parent element of activity's content view (that is, in Main. in XML, you can use <merge/> to replace the <framelayout/> label. The official document explains that this can reduce the level-1 layout level to optimize the layout. This is the original article about this part of the conclusion in the document. The Personal e level is limited and it is directly posted:
Obviusly, using <merge/> works in this case because the parent of an activity's content view is always a framelayout. you cocould not apply this trick if your layout was using a linearlayout as its root tag for instance.

For the use of merge and include tags, use the instance description directly.
Testmergeinclude. Java
Public class testmergeinclude extends activity {
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
}
}

Main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Merge
Xmlns: Android = "http://schemas.android.com/apk/res/android"
Xmlns: okcancelbar = "http://schemas.android.com/apk/res/test.mergeinclude">

<Imageview
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"

Android: scaletype = "center"
Android: src = "@ drawable/wallpaper_rainbokeh"/>

<Test. mergeinclude. okcancelbar
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "bottom"

Android: paddingtop = "8dip"
Android: gravity = "center_horizontal"

Android: Background = "# aa000000"

Okcancelbar: oklabel = "save"
Okcancelbar: cancellabel = "don't save"/>
</Merge>

Okcancelbar. Java
Public class okcancelbar extends linearlayout {

Public okcancelbar (context ){
Super (context );
// Todo auto-generated constructor stub
}

Public okcancelbar (context, attributeset attrs ){
Super (context, attrs );
// Todo auto-generated constructor stub

Setorientation (horizontal );
Setgravity (gravity. center );
Setweightsum (1.0f );

Layoutinflater. From (context). Inflate (R. layout. okcancelbar, this, true );

Typedarray array = context. obtainstyledattributes (attrs, R. styleable. okcancelbar, 0, 0 );

String text = array. getstring (R. styleable. okcancelbar_oklabel );
If (text = NULL) TEXT = "OK ";
(Button) findviewbyid (R. Id. okcancelbar_ OK). settext (text );

TEXT = array. getstring (R. styleable. okcancelbar_cancellabel );
If (text = NULL) TEXT = "cancel ";
(Button) findviewbyid (R. Id. okcancelbar_cancel). settext (text );

Array. Recycle ();
}
}

Okcancelbar. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Merge xmlns: Android = "http://schemas.android.com/apk/res/android">
<Include
Layout = "@ layout/okcancelbar_button"
Android: Id = "@ + ID/okcancelbar_ OK"/>

<Include
Layout = "@ layout/okcancelbar_button"
Android: Id = "@ + ID/okcancelbar_cancel"/>
</Merge>

Okcancelbar_button.xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Button xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

Think about it:
1. In the constructor of the okcancelbar class, we can see a little more complex code. In fact, they are not difficult, just get a specific button reference through the ID, and then set the text to be displayed for it. This setting is much more cumbersome than using Android: text directly. Therefore, we may consider this burden as a price to take advantage of the advantages of <merge/>. This advantage is Component reuse.
The okcancelbar_button.xml file declares a button, but it has two (more) buttons in use.

2. The definition of reusable components (okcancelbar_button here) is critical because it is associated with all functional points to use it. For example, if you are sure that in your application, all the display texts required for using this button are the same, you can add an android in the above button definition: text attribute, which saves the trouble of setting one by one during use. In addition, only one button is defined in okcancelbar_button in this example. I think it can be expanded to a layout unit, such as linearlayout and framelayout. I have not tried it yet. It is worth doing it.

3. Restrictions on using the <merge/> label:
(1) it can only be used as the root element of XML layout Declaration;
(2) When using it to inflate a layout, you must specify a viewgroup instance as its parent element and set the attachtoroot attribute to true (Refer to inflate (INT, Android. View. viewgroup, Boolean)Method description ). From: http://hi.baidu.com/lck0502/item/0b090cae8ac21b3e030a4d62

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.