The <merge/> label shines. When Layoutinflater encounters this tag, it skips over it and adds the elements inside the <merge/> to the parent element of the <merge/>.
Replace the Framelayout XML layout with <merge/>:
<Mergexmlns:android= "Http://schemas.android.com/apk/res/android"> <ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:scaletype= "Center"android:src= "@drawable/golden_gate" /> <TextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginbottom= "20dip"android:layout_gravity= "Center_horizontal|bottom"android:padding= "12dip"Android:background= "#AA000000"Android:textcolor= "#ffffffff"Android:text= "Golden Gate" /></Merge>
In the new code, TextView and ImageView are added directly to the framelayout in the previous layer. Although visually similar, the view hierarchy is simpler:
Obviously, using <merge/> On this occasion is because the Contentview parent of the activity is always framelayout.
If your layout uses linearlayout as its root tag (for example), then you cannot use this technique. <merge/> is also useful on other occasions.
For example, it can be perfectly combined with the <include/> tag. You can also use <merge/> When creating a custom combination view.
Let's look at an example of creating a new view using <merge/>--okcancelbar, which contains two buttons and can set the button label. The following XML is used to display a custom view on a picture:
<Mergexmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:okcancelbar= "Http://schemas.android.com/apk/res/com.example.android.merge"> <ImageViewAndroid:layout_width= "Fill_parent"Android:layout_height= "Fill_parent"Android:scaletype= "Center"android:src= "@drawable/golden_gate" /> <Com.example.android.merge.OkCancelBarAndroid: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>
The new layout effect looks like this:
The Okcancelbar code is simple because the two buttons are defined in an external XML file and imported through the Layoutinflate class. As shown in the following code snippet, R.layout.okcancelbar takes Okcancelbar as the parent element:
public class Okcancelbar extends LinearLayout {public Okcancelbar (context context, AttributeSet Attrs) { super ( context, attrs); 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 (); }}
The two buttons are defined as shown in the following XML. As you can see, we use the <merge/> tag to add two buttons directly to Okcancelbar.
Each button is included in the same XML layout file from the outside and is easy to maintain; we simply rewrite their IDs:
<Mergexmlns:android= "Http://schemas.android.com/apk/res/android"> <includeLayout= "@layout/okcancelbar_button"Android:id= "@+id/okcancelbar_ok" /> <includeLayout= "@layout/okcancelbar_button"Android:id= "@+id/okcancelbar_cancel" /></Merge>
We have created a flexible and easy-to-maintain custom view that has an efficient view hierarchy:
<merge/> tags are extremely useful. However it has also the following two restrictions:
- <merge/> can only be used as the root tag of an XML layout
- When you inflate a layout file that begins with <merge/>, you must specify a parent ViewGroup, and you must set Attachtoroot to True (see inflate (int, android.view.ViewGroup, Boolean) method).
Android's merge layout