zoukankan      html  css  js  c++  java
  • 使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.   

    效果截图:

      

  • 相关阅读:
    双六游戏 扩展欧几里得
    线段上的格点 辗转相除法(GCD)
    POJ 2395 Out of Hay
    POJ 2377 Bad Cowtractors
    POJ 1258 Agri-Net
    POJ 3169 Layout
    POJ 3723 Conscription
    次最短路径 POJ 3255 Roadblocks
    最小生成树求法 Prim + Kruskal
    如何将页脚固定在页面底部,4中方法 转载自:W3CPLUS
  • 原文地址:https://www.cnblogs.com/awkflf11/p/5094034.html
Copyright © 2011-2022 走看看