In Android layout files, there are many benefits of using the include tag at some point
1, for a slightly more complex layout interface, all layout code is written in an XML file, the interface will be very redundant, very poor readability, you can use the include tag separately to handle
2, when the activity needs to use the same layout effect, you can also use the Include tag processing, instead of the same layout code repeatedly copied several times, not to be modified every place to modify, improve the reusability of the Code
Let's first use the include tag to achieve the following effect
Activity_main.xml
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"Tools:context=". Mainactivity"> <TextView Android:id="@+id/text1"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="include how to use"android:gravity="Center"/> <include layout="@layout/LAYOUT_SEC1"Android:id="@+id/layout_sec1"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:layout_below="@id/text1"/> <include layout="@layout/layout_sec2"Android:id="@+id/layout_sec2"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:layout_below="@id/LAYOUT_SEC1"/> <include layout="@layout/LAYOUT_SEC3"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:layout_alignparentbottom="true"/></relativelayout>
You can see that 3 include tags are used in this layout file for 3 layouts, and when the include is called, the content of the corresponding subdivision layout file is completely embedded in the location specified by the include. The 3 layout codes are not posted.
This program only layout code, no Java code, relatively simple, you can implement or download the source code to see
It is important to note that there are often people using the include tag in relativelayout
However, it is found that the controls included in the include cannot be adjusted with tags such as layout_alignparentbottom= "true". In fact, the solution is very simple, as long as you in the include in the same time to overload the two tags layout_width and layout_height. If not overloaded, any layout adjustments to the include are not valid. It's better to experiment with the importance of overloading!
Here's a look at the second example.
Btn.xml
<?xml version="1.0"encoding="Utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="fill_parent"Android:layout_height="wrap_content"android:orientation="Vertical"> <Button Android:id="@+id/btn"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Button"> </Button> </LinearLayout>View.xml<!--<framelayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent">--> <merge xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView android:layout_width="match_parent"Android:layout_height="match_parent"Android:background="#FF0000"/></merge>
Activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Xmlns:tools="Http://schemas.android.com/tools"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"Tools:context=". Mainactivity"> <include android:id="@+id/layout1"layout="@layout/btn"/> <include android:id="@+id/layout2"layout="@layout/btn"/> <include layout="@layout/view"/> </LinearLayout>
You can see that the merge tag is used instead of the framelayout in View.xml, because the root tag of the layout is framelayout, so you can replace it with the merge tag, If the root tag is linearlayout, you cannot use merge. The advantage of using merge is that the UI structure is optimized, and the imported XML is represented by the merge as the root node, so that when it is embedded in the parent structure, it is good to dissolve the subset it contains into the parent structure. Instead of redundant nodes, interested friends can see the current allocation of UI resources through the Hierarchyviewer tool.
Then look at the Java code
Package Huahua.include2;import Android.os.bundle;import android.app.activity;import android.view.menu;import Android.view.view;import Android.widget.button;import android.widget.LinearLayout; Public classMainactivity extends Activity {PrivateLinearLayout LAYOUT1 =NULL; PrivateLinearLayout Layout2 =NULL; PrivateButton btn1; PrivateButton btn2; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LAYOUT1=(LinearLayout) Findviewbyid (R.ID.LAYOUT1); Layout2=(LinearLayout) Findviewbyid (R.ID.LAYOUT2); BTN1=(Button) Layout1.findviewbyid (R.ID.BTN); Btn1.setonclicklistener (NewBtnclick ()); BTN2=(Button) Layout2.findviewbyid (R.ID.BTN); Btn2.setonclicklistener (NewBtnclick ()); } Private classBtnclick implements view.onclicklistener{@Override Public voidOnClick (View v) {if(V.equals (BTN1)) {Btn1.settext ("Click on the button that is the first one"); } Else if(V.equals (btn2)) {Btn2.settext ("Click on the second button"); } }} @Override Publicboolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }}
At this point, the same r.id.btn should be differentiated by different layout.
Code: here
Use of Android include and merge tags