Android layout optimization----viewstub, include, merge

Source: Internet
Author: User

1 Stubview

Function: The layout in the Stubview tag is loaded only when it is needed.

Note: The Stubview render load operation can only be performed once; Merge tags are not supported

Examples of Use:

(1) Layout referenced in Viewstub

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout android:id= "@+id/stublayout" xmlns:android= "http// Schemas.android.com/apk/res/android "android:orientation=" vertical "android:layout_width=" Match_parent "Android:        layout_height= "Match_parent" > <textview android:id= "@+id/sbtv" android:layout_width= "300DP" android:layout_height= "300DP" android:text= "This is sub text view"/></linearlayout>

(2) using viewstub

<viewstub android:id= "@+id/viewstub" android:layout_width= "100DP" android:layout_height= "100DP" Android:lay out= "@layout/sublayout"/>

(3) Rendering loading in Java code

Viewstub stub = (viewstub) Findviewbyid (r.id.viewstub); Stub.inflate (); TextView STUBTV = (TextView) Layout.findviewbyid (R.ID.SBTV); Stubtv.settext ("Hello stub! ");


2 Include tags

Function: Replaces the referenced layout with the position of the label in the current layout;

Note: The referenced layout is non-merge, and the ID property of the Inlude is overwritten with the ID of the top layout of the referencing layouts;

Example 1 Reference layout non-merge

(1) Reference layout

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/sharedlayout2 "android:layout_width=" 200DP "android:layout_height=" 200DP "Android:ba        Ckground= "@android: Color/black" > <textview android:id= "@+id/mytv" android:layout_width= "100DP" android:layout_height= "100DP" android:background= "@android: Color/holo_blue_light" android:text= "at this time a common layout "/></linearlayout>

(2) using the Include tag

<include android:id= "@+id/include1" <!--the ID overrides linearlayout id;--> layout= "@layout/my" in the layout (1) android:layou T_width= "50DP" android:layout_height= "50DP" ></include>

(3) using in Java

LinearLayout sharedlayout = (linearlayout) Findviewbyid (R.ID.INCLUDE1); TextView TV = (TextView) Sharedlayout.findviewbyid (R.ID.MYTV); Tv.settext ("Hello Include1");

Example 2 referencing the merge layout

(1) Reference layout

<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >    < Linearlayout        android:id= "@+id/mergetlayout"          android:layout_width= "Wrap_content"          android:layout_height= "Wrap_content" >        <textview             android:id= "@+id/mergetv1"              android:layout_width= "40DP"              android:layout_height= "40DP"              android:background= "@android: Color/holo_blue_bright"              android:text= "Merge text1"  />       &nbSp; <textview            android:id= "@+id/ MERGETV2 "            android:layout_width=" 40DP "             android:layout_height= "40DP"              android:background= "@android: Color/darker_ Gray "            android:text=" Merge text2 "  />    </LinearLayout></merge>

(2) Include tag: the merge layout does not have an id attribute, so the id here actually doesn't make sense

<include android:id= "@+id/include2" layout= "@layout/mymerge" ></include>

(3) using in Java

LinearLayout layout = (linearlayout) Findviewbyid (R.ID.INCLUDE2);//Cannot get through this code (1) In the LinearLayout, the layout here is Nulltextview mTV1 = (TextView) Findviewbyid (R.ID.MERGETV1); Mtv1.settext ("Hello merge TV1 "); TextView MTV2 = (TextView) Findviewbyid (R.ID.MERGETV2); Mtv2.settext ("Hello merge tv2");

3 Merge Tab

The merge label is equivalent to a simple collection of controls, and is laid out after being put into other layouts, based on the container layout associated with each control.

Note: The merge tag does not have an id attribute

Examples of Use:

(1) Merge layout

<merge xmlns:android= "Http://schemas.android.com/apk/res/android" >    < Textview        android:id= "@+id/mergetv1"          android:layout_width= "40DP"         android: layout_height= "40DP"         android:background= "@android: Color/holo_ Blue_bright "        android:text=" Merge text1 " />     <textview        android:id= "@+id/mergetv2"          android:layout_width= "40DP"          android:layout_height= "40DP"         android:background= "@ Android:color/darker_gray "        android:text=" Merge text2 "  /></merge>

At this time the layout is two TextView overlap and MERGETV2 is in the upper layer;

(2) Merge label use

<include android:id= "@+id/include2" layout= "@layout/mymerge" ></include>

(3) Since the include tag is in a vertical linearlayout, the merge layout appears as follows:

Merge Text 1
Merge Text 2
(4) using in Java
TextView mTV1 = (TextView) Findviewbyid (R.ID.MERGETV1); Mtv1.settext ("Hello merge tv1"); TextView MTV2 = (TextView) Findviewbyid (R.ID.MERGETV2); Mtv2.settext ("Hello merge tv2");


This article is from the "Wauoen" blog, make sure to keep this source http://7183397.blog.51cto.com/7173397/1847175

Android layout optimization----viewstub, include, merge

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.