[Android] Android Layout Optimization Viewstub

Source: Internet
Author: User

Reprint please mark: reproduced in http://www.cnblogs.com/Liuyt-61/p/6602926.html

----------------------------------------------------------------

> Lazy Loading with Viewstub

Role: The viewstub tag can be used as an include tag to introduce an external layout, but the layout introduced by Viewstub does not expand by default, does not occupy the display, and does not occupy the location, thus saving CPU and memory when parsing layout.

Common_text.xml<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <TextView android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Hide Content"/></linearlayout>------------------------------------------------Main.xml<?xml version="1.0"encoding="Utf-8"? ><linearlayout xmlns:android="http://schemas.android.com/apk/res/android"Android:layout_width="match_parent"Android:layout_height="match_parent"android:orientation="Vertical"> <include layout="@layout/common_title"/> <framelayout android:layout_width="match_parent"Android:layout_height="wrap_content"> <TextView Android:id="@+id/textview"Android:layout_width="match_parent"Android:layout_height="wrap_content"Android:text="Body Content"android:textsize="16sp"/> <include layout="@layout/common_progress"/> </FrameLayout> <Button Android:id="@+id/button"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:text="Show hidden content"/> <viewstub Android:id="@+id/viewstub"Android:layout_width="wrap_content"Android:layout_height="wrap_content"Android:layout="@layout/common_text"/></linearlayout>--------------------------------------mainactivity.javapackage com. Liuyt.s03_e22_include;import Android.app.activity;import Android.os.bundle;import Android.view.View;import Android.view.view.onclicklistener;import Android.view.viewstub;import Android.widget.Button; Public classMainactivity extends Activity {PrivateButton btn; Privateviewstub viewstub; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); BTN=(Button) Findviewbyid (R.id.button); Viewstub=(viewstub) Findviewbyid (r.id.viewstub); Btn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method Stubviewstub.inflate ();    }        }); }}

[Android] Android Layout Optimization Viewstub

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.