Android Layout Optimization

Source: Internet
Author: User

When defining Android layout (XML), it is very important to have four special labels, three of which are related to resource reuse, respectively <viewstub/>, <requestfocus/>, <merge/> and <include/>. However, examples in previous cases or official documents do not focus on the importance of these tags.

1 .) <viewstub/>: this label allows the UI to visually display the effect similar to setting the invisible view in special cases, but it is larger (R) it means that the views wrapped by this label do not occupy any memory space by default. Viewstub imports the views element from the outside through include. Usage: Android: layout is used to specify the contained content. By default, all the labels contained in viewstub belong to visibility = gone. Viewstub calls the system to load its internal views through the inflate () method.

<Viewstub Android: Id = "@ + ID/stub"

Android: inflatedid = "@ + ID/subtree"

Android: Layout = "@ layout/mysubtree"

Android: layout_width = "120dip"

Android: layout_height = "40dip"/>

2.) <include/>: You can use this label to directly load external XML into the current structure, which is a common tag for reusing UI resources. Usage: Assign the XML file path that needs to be reused to the layout attribute of the include tag.

<Include Android: Id = "@ + ID/cell1" layout = "@ layout/ar01"/>

<Include Android: layout_width = "fill_parent" layout = "@ layout/ar02"/>

3.) <requestfocus/>: The tag is used to specify the focus view on the screen. Usage: place the tag inside the view tag

<Edittext id = "@ + ID/text"

Android: layout_width = "fill_parent"

Android: layout_height = "wrap_content"

Android: layout_weight = "0"

Android: paddingbottom = "4">

<Requestfocus/>

</Edittext>

4.) <merge/>:

The <merge/> label is introduced separately because it plays an important role in optimizing the UI structure. The objective is to optimize the entire android layout structure by deleting redundant or additional layers.

We will use an example to understand the actual functions of this tag, so that we can more intuitively understand the usage of <merge/>.

Create a simple layout with two views elements: imageview and textview. By default, these two elements are placed in framelayout. The effect is to display an image in full screen in the main view, and then display the title on the image, which is located below the view. Below is XMLCode:

<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/golden_gate"/>

<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 Gate"/>

</Framelayout>

The layout running view on the application is as follows:

Start tools> hierarchyviewer. BAT to view the current UI Structure View:

We can see that two framelayout nodes appear in the structure contained by the red box, obviously, these two nodes with the same meaning cause a waste of resources (here we can remind you that you can habitually view the current UI resource allocation through hierarchyviewer in the Development Project ), so how can we solve this problem (in the current example, how can we remove redundant framelayout nodes )? In this case, the <merge/> label is used to solve similar problems. We replace framlayout in the above XML code with merge:

<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/golden_gate"/>

<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 Gate"/>

</Merge>

ShippingProgramThe effect shown in emulator is the same, but the UI structure viewed through hierarchyviewer has changed. The redundant framelayout nodes were merged, you can also add the subset of the merge tag to the framelayout and node of the activity directly. (note the following:The root node of all activity views is framelayout.). If the created layout does not use framlayout as the root node (but uses linerlayout to define root labels), you cannot use the preceding example to optimize the UI structure through merge.

Besides the preceding example, meger has another usage.

When the include or viewstub label is used to import the XML structure from outside, the imported XML can be represented by merge as the root node, in this way, after being embedded into the parent structure, the subset it contains can be well integrated into the parent structure without redundant nodes.

Pay special attention to the following two points:

can only be used as the root node of XML layout. When the XML layout to be expanded is the root node of merge, You need to place the imported XML layout in the viewgroup, and set attachtoroot to true.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.