這是官方介紹:html
A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate()
is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. 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's inflatedId property. For instance:java
<ViewStub android:id="@+id/stub"
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
android:layout_width="120dip"
android:layout_height="40dip" />
The ViewStub thus defined can be found using the id "stub." After inflation of the layout resource "mySubTree," the ViewStub is removed from its parent. The View created by inflating the layout resource "mySubTree" can be found using the id "subTree," specified by the inflatedId property. The inflated View is finally assigned a width of 120dip and a height of 40dip. The preferred way to perform the inflation of the layout resource is the following:android
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
When inflate()
is invoked, the ViewStub is replaced by the inflated View and the inflated View is returned. This lets applications get a reference to the inflated View without executing an extra findViewById().web
下面是我從別人那裏學來的:app
在實際開發中,咱們常常會在佈局中準備多個View,默認但願都是不顯示,在程序運行時會根據條件來控制某個View顯示,
這樣最簡單的作法就是使用View.GONE的屬性隱藏。可是這樣帶來了問題,有些View可能一直都沒使用,但卻又佔用了內存,由於即便設置爲View.GONE的View仍然會
被實例化。解決這個問題,用ViewStub就好了。ViewStub是一個不可見,佔用資源很是小的View,可以用於在程序運行時延遲加載佈局。當inflate包含ViewStub的佈局時,
只實例化了這個ViewStub,其所指向的佈局並無被實例化。當這個ViewStub被設置visible,或者調用inflate方法時才實例化它指向的佈局的View和顯示ide
package com.viewstub.carlos.viewstubtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewStub; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button mShowButton; private Button mHideButton; private ViewStub mViewStub; private TextView mTextView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewStub = (ViewStub)findViewById(R.id.stub); //實例化佈局添加到ViewStub顯示 mViewStub.inflate(); mShowButton = (Button)findViewById(R.id.show_viewstub); mHideButton = (Button)findViewById(R.id.hide_viewstub); mShowButton.setOnClickListener(new showViewStubButtonListener()); mHideButton.setOnClickListener(new hideViewStubButtonListener()); mTextView1 = (TextView)findViewById(R.id.textView); mTextView1.setText(R.string.text1); } class showViewStubButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { mTextView1.setText(R.string.subtree1); //設置可見也能實例化佈局 mViewStub.setVisibility(View.VISIBLE); } } class hideViewStubButtonListener implements View.OnClickListener{ @Override public void onClick(View v) { mViewStub.setVisibility(View.GONE); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
整個工程文件:佈局