Friday, May 4, 2012

Android - How to merge two layout XML in Android?


Android - How to merge two layout XML in Android?



In Android, we can merge two seperate layout XML's into a single XML. It is done using and tags.
  - The tag does exactly what its name suggests; it includes another XML layout, i.e., means takes that file and paste its contents here. In the only the layout attribute is required. The attribute, without the android prefix, is a reference to the layout file you wish to include.
- The layout which we have to use must be enclosed under tag, so that we can include layouts from other xmls. The tag was created for the purpose of optimizing Android layouts by reducing the number of levels in view trees.
Ex:
Let's create a new Android project and add new Android XML file into "res/layout" folder and name it as "main1".
Now we have two layout main.xml and main1.xml as shown below.
Now we will merge these two layout XML into single layout.
main.xml
Above code  you can see that we included a xml file named main1.xml
main1.xml
Build and Run the project. You will see the merged layout like this.



No comments: