使ViewStub 来提高UI的加载的性能

 

  首先看下API中的ViewStub

    

          根据的文档的说明,ViewStub是一种默认不可见的试图,它没有大小,所以不能被改变,也不能通过某些把viewstub添加到布局当中来,

   不过我们可以使用inflate()来吧ViewStub中的试图增加进行,这样可以实现动态的添加试图,不必要每次在onCreate()的时候就加载布局,可以提高我们的性能。

    

    Demo中的使用方法:

     1:新建布局文件 设置<ViewStub>节点

     2: 在Activity中进行按钮点击  viewStub = (ViewStub) findViewById(R.id.mystub);

     3: View view = viewStub.inflate();  把ViewStub中的View增添进来

  

  下面Demo源代码:

  主Activity类:

   

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

 

 

  布局文件:

   main.xml:

   

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="点击确定" />  
  12.   
  13.     <ViewStub  
  14.         android:id="@+id/mystub"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout="@layout/demo_viewstub" >  
  18.     </ViewStub>  
  19.   
  20. </LinearLayout>  

  demo_viewstub.xml:
  

[html] view plaincopy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/layout"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <TextView  
  9.         android:id="@+id/tv"  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="ViewStubDemo_Byjiangqq" />  
  13.   
  14. </LinearLayout>  
[html] view plaincopy
 
  1.   
[html] view plaincopy
 
  1.   

 

效果截图:

  

posted on 2016-01-02 00:36  左手指月  阅读(212)  评论(0编辑  收藏  举报