Android component Viewstub Basic use method detailed _android

Source: Internet
Author: User

Viewstub can dynamically add layouts at run time. The help document is given a definition of:

"A Viewstub is a invisible, zero-sized View that can being used to lazily inflate layout resources at runtime." When a viewstub was made visible, or when inflate () was invoked, the layout resource is inflated. The viewstub then replaces itself in it parent with the inflated View. Therefore, the viewstub exists in the view hierarchy until setvisibility (int) or inflate () is invoked. The inflated View is added to the viewstub's parent with the Viewstub ' s layout parameters. Similarly, you can define/override the inflate View ' s ID by using the Viewstub ' Inflatedid property.

Allin all: Viewstub can give other view prior positions, call Inflater () or the setvisible () method when needed to display these view components.

Layout.xml:

<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:o" rientation= "Vertical" tools:context= ".
    Mainactivity "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content " android:orientation= "Horizontal" > <button android:id= "@+id/showbtn" android:layout_width= "Match_" Parent "android:layout_height=" 50dip "android:layout_weight=" 1 "android:text=" show "/> <butto n android:id= "@+id/deletebtn" android:layout_width= "match_parent" android:layout_height= "50dip" an droid:layout_weight= "1" android:text= "delete"/> </LinearLayout> <linearlayout Android:layout_w
      Idth= "Wrap_content" android:layout_height= "wrap_content" > <viewstub android:id= "@+id/viewstub" Android:layouT_width= "Wrap_content" android:layout_height= "wrap_content" android:layout= "@layout/next"/> </Li
 Nearlayout> </LinearLayout>

Next.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:o" rientation= "vertical" > <progressbar android:id= @+id/progressbar1 "style="
    ? android:attr/ Progressbarstylelarge "
    android:layout_width=" wrap_content "
    android:layout_height=" wrap_content "/>"

</LinearLayout>

Main.java:

Package com.example.android_viewstub1;
Import Android.os.Bundle;
Import Android.annotation.SuppressLint;
Import android.app.Activity;
Import Android.view.Menu;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import android.view.ViewStub;

Import Android.widget.Button;
  public class Mainactivity extends activity {Button btn1, btn2;

  Viewstub viewstub;
    @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

    Setcontentview (R.layout.activity_main);
    BTN1 = (Button) This.findviewbyid (R.ID.SHOWBTN);
    BTN2 = (Button) This.findviewbyid (R.ID.DELETEBTN);
    Viewstub = (viewstub) This.findviewbyid (r.id.viewstub); Btn1.setonclicklistener (New Onclicklistener () {@SuppressLint ("Newapi") @Override public void OnClick (Vi
      EW arg0) {viewstub.setvisibility (view.visible);
    }

    });
  Btn2.setonclicklistener (New Onclicklistener () {@SuppressLint ("Newapi") @Override    public void OnClick (View arg0) {viewstub.setvisibility (view.invisible);

  }

    });
 }

}

Effect:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.