1. 概述:

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

2. 效果图:

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

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

3. 实现代码:

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

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

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

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

(4)主Activity实现:

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

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

Android中ViewStub组件使用的更多相关文章

  1. android中四大组件之间相互通信

    好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...

  2. Android中Intent组件详解

    Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...

  3. Android中四大组件总结

    android四大组件分别为activity.service.content provider.broadcast receiver. 一.android四大组件详解 1.activity (1)一个 ...

  4. Android中BroadcastReceiver组件具体解释

    Android系统的4个组件最终还剩一种组件了BroadcastReceiver,这个组件是全局监听器,能够监听系统全局的广播消息,能够方便的实现系统中不同组件之间的通信 BroadcastRecei ...

  5. Android笔记(六) Android中的组件

    一个软件可以吸引到用户,除了优秀的功能可以解决用户的问题之外,良好的用户界面也并不可少.一个软件的功能不管多么优秀,但是没有提供友好的界面来让用户操作,将很难吸引到最终用户. Android提供了大量 ...

  6. Android中各组件的生命周期

    1.Activity生命周期图 二.activity三种状态 (1)active:当Activity运行在屏幕前台(处于当前任务活动栈的最上面),此时它获取了焦点能响应用户的操作,属于活动状态,同一个 ...

  7. Android中四大组件

    Activity  BroadCast Receiver 广播接收者 Service  服务 Content Provider 内容提供者   四大组件都需要在清单文件里面配置一下

  8. android中各种组件的生命周期问题

    1,activiy生命周期 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/ 结合ativity的状态转换来看才 ...

  9. Android中GridView通过自定义适配器(未优化)实现图文视图排列

    Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...

随机推荐

  1. 工作中的第一份LoadRunner脚本

    录制的第一份脚本 虽然是第一份但是调试执行,跑场景等都成功了. Action() { web_url("login1.jsp", "URL=http://192.168. ...

  2. JDK Debug

    http://ishare.iask.sina.com.cn/f/23897007.html http://hi.baidu.com/bd_hare/item/7edd0415b60f0101e65c ...

  3. PBOC2.0安全系列之—脱机认证之动态数据认证(DDA)

    动态数据认证: 一,什么是动态数据认证(DDA) 由于上篇<< PBOC2.0安全系列之—脱机认证之静态数据认证(SDA)>>已经对静态数据认证部分做了详细的分析,一些基本知识 ...

  4. POJ——多项式的加法

    1:多项式加法 查看 提交 统计 提问 总时间限制:  1000ms  内存限制:  5000kB 描述 我们经常遇到两多项式相加的情况,在这里,我们就需要用程序来模拟实现把两个多项式相加到一起.首先 ...

  5. YBC中国国际青年创业计划

    YBC中国国际青年创业计划 中国青年创业国际计划(简称YBC)是共青团中央.中华全国青年联合会.中华全国工商业联合会共同倡导发起的青年创业教育项目.该项目参考总部在英国的青年创业国际计划( Youth ...

  6. linux(debian) 安装jdk

    #vi /etc/profile 在里面添加如下内容 export JAVA_HOME=/usr/java/jdk1.6.0_27 export JAVA_BIN=/usr/java/jdk1.6.0 ...

  7. Android studio 安装,JDK 出错解决方案

    在安装android studio 的时候,会报一个错误: --------------------------- Error launching Android Studio ----------- ...

  8. req.xhr在express中的应用

    req.xhr判断请求来自ajax还是普通请求: 若为ajax则是为true 这个属性是通过判断headers中的 x-requested-with的值来判断的 下面是来自ajax的请求: 1 hos ...

  9. python升级导致的坑

    问题来源 问题往往都是这样来的突然,让我措手不及. 小孩没娘说来话长啊,操作系统是centos6.5因此默认自带的python是2.6.6的,突然有一天我要写一个关于kafka topic消费情况的监 ...

  10. poj 2728 Desert King(最优比例生成树)

    #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #i ...