How to use Android viewstub

Source: Internet
Author: User

when you write a project, there must be something written in advance, but without a certain condition you don't want it to show up, we might do it by making it view. gone or view . INVISIBLE wait for a certain condition in the code this setting view Although this is also very convenient to write, but also logical, but this condition is 800 years do not come out once, you write a little bit big only small use, because you pre-layout good inflate, i.e. that is, the object will still be created, it will be instantiated, and the property will be set. In other words, it consumes resources such as memory.

the recommended practice is to use android.view.ViewStub , Viewstub is a lightweight view that is invisible, occupies no layout position, and occupies very small resources of the control. You can specify a layout for viewstub, in inflate layout, only viewstub is initialized, and when Viewstub is set to visible, or when viewstub.inflate () is called, The layout viewstub is inflate and instantiated, and the layout properties of the Viewstub are passed to the layout that it points to. In this way, you can use viewstub to make it easier to run, or not to display a layout.

But Viewstub is not omnipotent, the following summarizes what viewstub can do and when to use the viewstub, when the visibility of the control.

First of all, some features of viewstub:

1. Viewstub can only be inflate once, and then viewstub objects will be empty. According to the sentence, a viewstub specified by the layout is inflate, it will not be enough to control it through the viewstub.

2. Viewstub can only be used to inflate a layout file, not a specific view, of course, you can also write the view in a layout file.

Based on the above characteristics, the use of viewstub can be considered as follows:

1. During the run of the program, a layout will not change after inflate, unless it is restarted.

Because viewstub can only be inflate once and then be empty, you cannot expect to use Viewstub to control the layout later. So when you need to show and hide a layout more than once at run time, Viewstub can't. You can only use the visibility of the view to control it.

2. You want to control what is displayed and hidden is a layout file, not a view.

Because the ID of a layout file can only be set to viewstub, it cannot be controlled for a view.

So, if you want to control the display and concealment of a view (such as button or TextView), or if you want to show and hide a layout or view at run time, you can only use the visibility of the view to control it.

Here is an example, this example is, we first use viewstub XML cloth, which obviously will not be inflate, we in the code to show him, but suddenly the condition has not met, I must put the display of things to hide it, but this time Viewstub has been destroyed, can no longer be used, this time we have to use gone and visible to control the display and hide.

First of all we set the three xml:

Main xml:

<relativelayout 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 "Tools:context =". Mainactivity "><viewstub android:id=" @+id/viewstub1 "android:layout_width=" Wrap_content "android:layout_he ight= "Wrap_content" android:layout_marginleft= "5dip" android:layout_marginright= "5dip" Android:layout_margi ntop= "10dip" android:layout= "@layout/layout1"/><viewstub android:id= "@+id/viewstub2" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_marginleft= "5dip" Android:layout_marginr ight= "5dip" android:layout_margintop= "10dip" android:layout= "@layout/layout2"/><button android:id= "@+id /btn_show "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "Android:layout_alignParentBot Tom= "true" Android:layout_centerhorizonTal= "True" android:text= "click I show hidden layouts"/></relativelayout> 
The rest is the XML that needs to be loaded

<?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" >    <textview        android:id= "@+id/textview1" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:text=" I was a hidden layout "/></linearlayout>
<?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" >    <imageview        android:id= "@+id/imageview1" android:layout_width= "Wrap_        Content "        android:layout_height=" wrap_content "        android:src=" @drawable/ic_launcher "/></ Linearlayout>
Let's look at the main code:

Package Com.example.viewstub;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;import Android.widget.toast;public class Mainactivity extends Activity {private viewstub viewstub1;private viewstub viewStub2; Private Button Btnshow;private Boolean b=true;private boolean a=true, @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitView ();} private void Initview () {viewstub1= (viewstub) Findviewbyid (R.ID.VIEWSTUB1); viewstub2= (viewstub) Findviewbyid ( R.ID.VIEWSTUB2); btnshow= (Button) Findviewbyid (r.id.btn_show); Btnshow.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {if (b) {//condition conforms to display layout viewstub1.inflate (); Viewstub2.inflate (); b=false;} Else{if (a) {//condition does not conform to this viewstub has been recycled so we can only use GONE and visibleviewstub2.setvisibility (view.gone); viewstub1.setvisibility (View.gone); a=false;} else{//conditions conform to viewstub2.setvisibility (view.visible); viewstub1.setvisibility (view.visible); a=true;}}});}}
This compares with the conditions that appear in the trial 800 years.
The code is too simple to paste the effect!







How to use Android 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.