When you develop an application, you often encounter situations where you can dynamically determine which view or layout is displayed based on criteria at run time. The most common idea is to write all of the views you might use on them, first set their visibility to view.gone, and then dynamically change its visibility in the code. The advantage of this approach is that logic is simple and control is more flexible. But its disadvantage is that it consumes resources. Although the view is initially visible view.gone but the view will still be inflate when inflate layout, that is, the object will still be created, instantiated, and set to properties. In other words, it consumes resources such as memory.
The recommended practice is to use android.view.viewstub,viewstub to be a lightweight view, which is an invisible control that does not occupy the location of the layout and occupies a very small resource. You can specify a layout for viewstub, and only viewstub will be initialized when inflate layout, and then when Viewstub is set to be visible, or when viewstub.inflate () is invoked, The layout of the viewstub is inflate and instantiated, and then the Viewstub layout property is passed to the layout it points to. In this way, you can use viewstub to make it easy to show a layout at run time or not.
But Viewstub is not omnipotent, the following summary viewstub can do and when to use Viewstub, when the visibility of the control.
First, some features of viewstub:
1. Viewstub can only be inflate once, after which Viewstub object will be set to null. In other words, a layout specified by Viewstub is inflate, and it will not be enough to control it by Viewstub.
2. Viewstub can only be used to inflate a layout file, not a specific view, and of course, you can also write the view in a layout file.
Based on the above characteristics, then you can consider the use of viewstub:
1. During the operation of a 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 do it. This can only be controlled using the visibility of the view.
2. Want to control the display and hidden is a layout file, not a view.
Because the ID of a layout file is set to Viewstub, it cannot be allowed to control a view.
So, if you want to control the display and hiding of a view (such as a button or textview), or if you want to show and hide a layout or view at run time, you can only control it using the visibility of the view.
Let's look at an example
In this example, to display two different layouts, one is to display a text with TextView, and the other is to display a picture with ImageView. These two are the OnCreate () when deciding which one to display, and here is the best place to apply viewstub.
First look at the layout, one is the main layout, which only defines two viewstub, one to control the TextView one to control the ImageView, another is to display the text to do the TextView layout, one is for the ImageView layout:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:gravity= "Center_horizontal" >
<viewstub
Android:id= "@+id/viewstub_demo_text"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "5dip"
android:layout_marginright= "5dip"
android:layout_margintop= "10dip"
android:layout= "@layout/viewstub_demo_text_layout"/>
<viewstub
Android:id= "@+id/viewstub_demo_image"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
android:layout_marginleft= "5dip"
android:layout_marginright= "5dip"
android:layout= "@layout/viewstub_demo_image_layout"/>
</LinearLayout>
layout for TextView:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<textview
Android:id= "@+id/viewstub_demo_textview"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:background= "#aa664411"
Android:textsize= "16sp"/>
</LinearLayout>
layout for ImageView:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout
Xmlns:android= "Http://schemas.android.com/apk/res/android"
android:orientation= "Vertical"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" >
<imageview
Android:id= "@+id/viewstub_demo_imageview"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"/>
</LinearLayout>
Let's look at the code, decide which one to show, just find the appropriate viewstub and call its infalte () to get the desired layout:
Copy Code code as follows:
Package com.effective;
Import android.app.Activity;
Import Android.os.Bundle;
Import android.view.ViewStub;
Import Android.widget.ImageView;
Import Android.widget.TextView;
public class Viewstubdemoactivity extends activity {
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.viewstub_demo_activity);
if (((int) (Math.random () *) & 0x01) = = 0) {
To show text
All your have to be inflate the viewstub for TextView
Viewstub stub = (viewstub) Findviewbyid (R.id.viewstub_demo_text);
Stub.inflate ();
TextView Text = (TextView) Findviewbyid (R.id.viewstub_demo_textview);
Text.settext ("The Tree of Liberty must is refreshed from time to time" +
"With the blood of Patroits and tyrants! Freedom is nothing but +
"A chance to be better!");
} else {
To show image
All your have to be inflate the viewstub for ImageView
Viewstub stub = (viewstub) Findviewbyid (r.id.viewstub_demo_image);
Stub.inflate ();
ImageView image = (ImageView) Findviewbyid (R.id.viewstub_demo_imageview);
Image.setimageresource (R.drawable.happy_running_dog);
}
}
}
Run Result:
Precautions to use:
1. Some layout attributes should be added to the viewstub rather than the actual layout to work, such as the android:layout_margin* series attributes used above, if added to the TextView, it will not work, You need to put it on the viewstub to make it work. The viewstub properties are passed to the appropriate layout after inflate () .