Content transferred from: http://fengweipeng1208.blog.163.com/blog/static/21277318020138229754135/
When we develop the Android layout, there are often a lot of layouts are the same, this time we can use the <include/> and <merge/> tag implementation of the complex layout to include in the required layout, reduce the writing of repetitive code.
1. Create a layout that you can reuse:
The following code describes the top bar that appears for each acitivity in your app Titlebar.xml
1 <Framelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 Android:layout_width= "Match_parent"3 Android:layout_height= "Wrap_content"4 Android:background= "@color/titlebar_bg">5 6 <ImageViewAndroid:id= "@+id/title"7 Android:layout_width= "Wrap_content"8 Android:layout_height= "Wrap_content" 9 android:src= "@drawable/gafricalogo" />Ten </Framelayout>
The root layout above (root view) is where the framelayout appears after it is inserted.
2. Use <include/> Tags:
In the application of an activity in the layout of the top bar is the above layout, then we can include the above titlebar.xml to achieve the effect of reuse, layout code is as follows:
1 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"2 android:orientation= "vertical" 3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 Android:background= "@color/app_bg"6 android:gravity= "Center_horizontal">7 8 <includeAndroid:id= "@+id/new_title"9 Layout= "@layout/titlebar"/>Ten One <TextViewAndroid:layout_width= "Match_parent" A Android:layout_height= "Wrap_content" - Android:text= "@string/hello" - android:padding= "10DP" /> the - ... - - </LinearLayout>
By getting the ID of the element, we can modify the attributes of the element in the include tag, and the following example modifies the image in the titlebar in actiivty:
1 Private NULL ; 2 Private NULL ; 3 4 Mtitlebar = Findviewbyid (r.id.new_title); 5 Mtitleimageview = (ImageView) Mtitlebar.findviewbyid (r.id.title); 6 Mtitleimageview.setimageresource (R.drawable.logo);
When using the include tag, we can overwrite the properties of the Insert Layout root view (all android:layout_* properties):
1 < android:id= "@+id/news_title"2 android:layout_width= "Match _parent "3 android:layout_height=" Match_parent "4 Layout= "@layout/title"/>
If you need to overwrite the properties of the Insert Layout root view, you must make both the Android:layout_width and Android:layout_height properties for the other overwrite properties to take effect.
Note: Only the properties of the referenced layout, the outer layout property can be modified, and the internal properties cannot be modified.
For example: Button2.xml, the content is as follows:
1 < button xmlns:android = "Http://schemas.android.com/apk/res/android" 2 Android:id = "@+id/button1" 3 android:layout_width = "Wrap_content" Android:layout_ Height = "Wrap_content" 5 Android:text = "button" />
Then refer to this layout elsewhere: <include android:id= "@+id/button1" android:layout_width= "Match_parent"android:layout_height= "Wrap_content"android:text= "Button23456"layout= "@layout/button2"/>
Then, the green Code setting of the property is valid, and the Red Code setting of the property is not valid.
In fact, it is easy to understand, that is, include is to reuse a package of good layout, then, the layout of the interior of the things naturally do not change, if you want to change, it is better to change in the referenced file, we have modified in the include only the size of the referenced layout location.
3. Use <merge/> tags
The Merge tab is used to eliminate the redundant view group that we produce when we include one layout to another. For example, there are two consecutive buttons in many layouts now, so we make the two successive buttons reusable (re-usable layout). When using the include tag, we must first organize the two buttons with a view group such as LinearLayout and then use it for other layouts. If it is part of the liearlayout, there will be a two-layer continuous liearlayout, except that there is no benefit in reducing UI performance. At this point we can use the <merge/> tag as the root view of the reusable layout to avoid this problem.
1 <Mergexmlns:android= "Http://schemas.android.com/apk/res/android">2 3 <Button4 Android:layout_width= "Fill_parent" 5 Android:layout_height= "Wrap_content"6 Android:text= "@string/add"/>7 8 <Button9 Android:layout_width= "Fill_parent" Ten Android:layout_height= "Wrap_content" One Android:text= "@string/delete"/> A - </Merge>
When we reuse the above code with the <include/> tag, the merge element is ignored and the two consecutive buttons are placed directly at the <include/> tag location.
"Go" using the Include and merge tags in android layouts