Android Merge Detailed

Source: Internet
Author: User

An example will be used to understand what the tag actually does, so you can get a more intuitive view of <merge/> usage.

Create a simple layout that contains two views elements: ImageView and TextView By default we place these two elements in Framelayout. The effect is to display a picture in full screen in the main view, then display the title on the picture and below it. The following is the XML code:

<?xml version= "1.0"  encoding= "Utf-8"? ><framelayout xmlns:android= "http// Schemas.android.com/apk/res/android "    android:layout_width=" Fill_parent "     android:layout_height= "Fill_parent" >     <ImageView         android:layout_width= "Fill_parent"          android:layout_height= "Fill_parent"         android: Scaletype= "center"         android:src= "@drawable/rain"  />      <textview        android: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 gate2"  / >      </FrameLayout>

The view used to run the above layout is:

Start the tools> Hierarchyviewer.bat tool to view the current UI structure view:

We can clearly see the emergence of two framelayout nodes, it is obvious that these two completely meaning the same node caused the waste of resources, then how to solve this problem (in the current example is how to remove the redundant framelayout node)? This is the time to use the <merge/> tag to deal with similar problems.

This reminds you of the habit of passing through the development project. Hierarchyviewer view the allocation of the current UI resource. The Hierarchy Viewer is a tool released with ANDROIDSDK, located under the Tools folder, named Hierarchyviewer.bat. It's a very useful and easy-to-use tool for Android, which helps us to better view and design the user interface (UI) and is definitely a tool for UI viewing. Specific can see this article Android tool hierarchy Viewer

We replace the framlayout in the above XML code with the merge:

<?xml version= "1.0"  encoding= "Utf-8"? ><merge xmlns:android= "http://schemas.android.com /apk/res/android ">     <ImageView         android:layout_width= "Fill_parent"         android:layout_height= "Fill_parent"         android:scaletype= "center"          android:src= "@drawable/rain"  />     < Textview        android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content"          android:layout_marginbottom= "20dip"         android:layout_ Gravity= "Center_horizontal|bottom"         android:padding= "12dip"       &nbsP; android:background= "#AA000000"         android:textcolor= "# FFFFFFFF "        android:text=" Golden gate2 " />       </merge>

UI structure diagram:

The effect shown in emulator after running the program is the same, but the UI structure viewed through hierarchyviewer is changed, and the redundant framelayout nodes are merged together. Or it can be understood that the subset of the merge tag is added directly to the activity's framelayout and nodes (it is important to note that the root node of all activity Views is framelayout). If you create a layout that does not use framlayout as the root node (instead of defining root tags such as linerlayout), you cannot use the above example to optimize the UI structure by using merge.

In addition to the above example, Meger has another usage, and when an include or viewstub tag is imported from an external XML structure, the imported XML can be represented as a root node in the merge. This way, when embedded in the parent structure, it is good to dissolve the subset it contains into the parent structure without the redundant nodes.


There are two additional points that require special attention:

1.<merge/> can only be used as the root node of XML layout.

2. When the XML layout that needs to be expanded itself is composed of the merge as the root node, the imported XML layout needs to be placed in the ViewGroup, and the attachtoroot must be set to true.


Android Merge Detailed

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.