• 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

  • 相关阅读:
    objective-c数组
    objective-c可变数组
    objective-c可变字典
    objective-c字典
    有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
    将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5
    一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?
    求123456789-23456789-3456789-456789-...-9的值
    编写一个程序,求s=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值
    Unity3D笔记 GUI 二 、实现选项卡一窗口
  • 原文地址:https://www.cnblogs.com/kuloud/p/3372833.html
Copyright © 2020-2023  润新知