Viewstub of Android Development layout optimization

Source: Internet
Author: User

Using viewstub can delay loading a layout file and increase the display rate. Just beginning to come into contact, record it.

With regard to the use of viewstub, we can use it in different layouts, such as dynamically determining which interface to display depending on the size of the device.

Viewstub and include are more likely to embed another layout file in one layout file, whereas Viewstub can be said to be lazy loading, which will only load the layout file when you manually specify the load, and the include will load immediately.

Using viewstub tags in layouts to introduce files

<linearlayout  xmlns: Android  = "http://schemas.android.com/apk/res/android"  xmlns:tools  =" Http://schemas.android.com/tools " android:layout_width  = "match_parent"  android:layout_height  =" match_parent " android:orientation  =" vertical " tools:context  = "com.example.viewstub.MainActivity" ;     <textview  android:layout _width  = "wrap_content"  android:layout_height  = "wrap_content"  android:text  = "@string/hello_world" />     <button  android:id  = "@+id/toggle"  android:layout_width  =         "wrap_content"  android:layout_height  = "wrap_content"  android:onclick  = "OnClick"  android:text  =/>     <viewstubandroid:id= "@+id/vs"android:layout_width="match_parent "android:layout_height="match_parent "android:inflatedid="@+id/inflated_id "  android:layout="@layout/view_stub_layout" />                                        </linearlayout>

View_stub_layout.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  =; <TextViewandroid:id= "@+id/tv"android:layout_width="match_parent "android:layout_height=" Wrap_content "android:text="TV in vs "/>                     </linearlayout>

This is the layout file, so how to load this layout when the program is running?

 Public  class mainactivity extends actionbaractivity {    PrivateViewstub stub;Private BooleanIsshow =true;PrivateTextView TV;/* (non-javadoc) * @see android.support.v7.app.actionbaractivity#oncreate (android.os.Bundle) * /    @Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Layout is loaded in two ways, one is stub.inflate ();        //Another is stub.setvisibility (view.visible);stub = (viewstub) This. Findviewbyid (R.id.vs);//Stub.inflate ();Stub.setvisibility (view.visible);//After instantiation, you can get the root node of the stub layout and then manipulate itView root = This. Findviewbyid (r.id.inflated_id);//Note to instantiate the stub before you can get the TVTV = (TextView) Root.findviewbyid (r.id.tv);    Root.setbackgroundcolor (Color.Blue); } Public void OnClick(View v) {Switch(V.getid ()) { CaseR.id.toggle:if(isshow)            {stub.setvisibility (View.gone); }Else{stub.setvisibility (view.visible); Tv.settext ("---"); } isshow =!isshow; Break;default: Break; }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If there is a wrong place, I would appreciate it if I could criticize it.

Viewstub of Android Development layout optimization

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.