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