Android中ViewStub组件使用

1. 概述:

    ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲染,而<include>标签中的布局会随着它所在布局的渲染而渲染,ViewStub中的布局只有在你需要的时候才会渲染到主界面中。

2. 效果图:

   (1)在ButtonOne与ButtonTwo之间存在一个ViewStub布局,如下图:

 ViewStub1

   (2)单击ButtonOne后渲染ViewStub中的布局,如下图:

ViewStub2

3. 实现代码:

    (1)res/layout/main.xml实现:

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"  
  4.     android:orientation = "vertical"  
  5.     android:layout_width = "fill_parent"  
  6.     android:layout_height = "fill_parent"  
  7.     >  
  8.       
  9.     <Button  
  10.         android:id = "@+id/show"  
  11.         android:text = "ButtonOne"  
  12.         android:layout_width = "wrap_content"  
  13.         android:layout_height = "wrap_content"  
  14.         />  
  15.       
  16.     <ViewStub  
  17.         android:id = "@+id/viewStub"  
  18.         android:layout = "@layout/green_layout"  
  19.         android:layout_width = "300dip"  
  20.         android:layout_height = "300dip"  
  21.         />  
  22.           
  23.     <Button  
  24.         android:layout_width = "wrap_content"  
  25.         android:layout_height = "wrap_content"  
  26.         android:text = "ButtonTwo"  
  27.         />  
  28.       
  29. </LinearLayout>  

    (2)main.xml中ViewStub组件里的布局实现:

[java] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.     xmlns:android = "http://schemas.android.com/apk/res/android"  
  5.     android:layout_width = "match_parent"  
  6.     android:layout_height = "match_parent"  
  7.     android:background = "@color/green">  
  8.       
  9. </LinearLayout>  

    (4)主Activity实现:

[java] view plaincopy
 
  1. package com.focus.fishme;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.ViewStub;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class ViewStubActivity extends Activity {  
  11.       
  12.     private ViewStub mViewStub;  
  13.       
  14.     private Button mShow;  
  15.       
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.   
  21.         mViewStub = (ViewStub) findViewById(R.id.viewStub);  
  22.           
  23.         mShow = (Button) findViewById(R.id.show);  
  24.         mShow.setOnClickListener(new OnClickListener() {  
  25.             public void onClick(View view) {  
  26.                 if (mViewStub != null) {  
  27.                     mViewStub.inflate();  
  28.                 }  
  29.             }  
  30.         });  
  31.     }  
  32.       
  33. }  

 转自:http://blog.csdn.net/mayingcai1987/article/details/6238609

原文地址:https://www.cnblogs.com/kuloud/p/3372833.html