Android中ViewStub组件使用
1. 概述:
ViewStub组件和<include>标签的作用类似,主要是为了提高布局的重用性,及布局的模块化。它们之间最大的差别是,ViewStub中的布局不会随着它所在布局的渲染而渲染,而<include>标签中的布局会随着它所在布局的渲染而渲染,ViewStub中的布局只有在你需要的时候才会渲染到主界面中。
2. 效果图:
(1)在ButtonOne与ButtonTwo之间存在一个ViewStub布局,如下图:

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

3. 实现代码:
(1)res/layout/main.xml实现:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
- android:orientation = "vertical"
- android:layout_width = "fill_parent"
- android:layout_height = "fill_parent"
- >
- <Button
- android:id = "@+id/show"
- android:text = "ButtonOne"
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- />
- <ViewStub
- android:id = "@+id/viewStub"
- android:layout = "@layout/green_layout"
- android:layout_width = "300dip"
- android:layout_height = "300dip"
- />
- <Button
- android:layout_width = "wrap_content"
- android:layout_height = "wrap_content"
- android:text = "ButtonTwo"
- />
- </LinearLayout>
(2)main.xml中ViewStub组件里的布局实现:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android = "http://schemas.android.com/apk/res/android"
- android:layout_width = "match_parent"
- android:layout_height = "match_parent"
- android:background = "@color/green">
- </LinearLayout>
(4)主Activity实现:
- package com.focus.fishme;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewStub;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- public class ViewStubActivity extends Activity {
- private ViewStub mViewStub;
- private Button mShow;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mViewStub = (ViewStub) findViewById(R.id.viewStub);
- mShow = (Button) findViewById(R.id.show);
- mShow.setOnClickListener(new OnClickListener() {
- public void onClick(View view) {
- if (mViewStub != null) {
- mViewStub.inflate();
- }
- }
- });
- }
- }
转自:http://blog.csdn.net/mayingcai1987/article/details/6238609
Android中ViewStub组件使用的更多相关文章
- android中四大组件之间相互通信
好久没有写有关android有关的博客了,今天主要来谈一谈android中四大组件.首先,接触android的人,都应该知道android中有四大组件,activity,service,broadca ...
- Android中Intent组件详解
Intent是不同组件之间相互通讯的纽带,封装了不同组件之间通讯的条件.Intent本身是定义为一个类别(Class),一个Intent对象表达一个目的(Goal)或期望(Expectation),叙 ...
- Android中四大组件总结
android四大组件分别为activity.service.content provider.broadcast receiver. 一.android四大组件详解 1.activity (1)一个 ...
- Android中BroadcastReceiver组件具体解释
Android系统的4个组件最终还剩一种组件了BroadcastReceiver,这个组件是全局监听器,能够监听系统全局的广播消息,能够方便的实现系统中不同组件之间的通信 BroadcastRecei ...
- Android笔记(六) Android中的组件
一个软件可以吸引到用户,除了优秀的功能可以解决用户的问题之外,良好的用户界面也并不可少.一个软件的功能不管多么优秀,但是没有提供友好的界面来让用户操作,将很难吸引到最终用户. Android提供了大量 ...
- Android中各组件的生命周期
1.Activity生命周期图 二.activity三种状态 (1)active:当Activity运行在屏幕前台(处于当前任务活动栈的最上面),此时它获取了焦点能响应用户的操作,属于活动状态,同一个 ...
- Android中四大组件
Activity BroadCast Receiver 广播接收者 Service 服务 Content Provider 内容提供者 四大组件都需要在清单文件里面配置一下
- android中各种组件的生命周期问题
1,activiy生命周期 http://www.ibm.com/developerworks/cn/opensource/os-cn-android-actvt/ 结合ativity的状态转换来看才 ...
- Android中GridView通过自定义适配器(未优化)实现图文视图排列
Android中GridView组件用来以网格方式排列视图,与矩阵类似,当屏幕上有很多元素(文字.图片或其他元素)需要显示时,可以使用该组件.下面我们通过代码实现如下图例(为了方便截图,将事件处理(土 ...
随机推荐
- php 数组去除空值
/** * 方法库-数组去除空值 * @param string $num 数值 * @return string */ public function array_remove_empty(& ...
- 超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上
超轻量级高性能ORM数据访问组件Deft,比dapper快20%以上 阅读目录 Deft简介 Deft 核心类介绍 Deft 3分钟即可上手使用 其他可选的配置参数 性能测试 Demo代码下载 回到顶 ...
- 算法分析-堆排序 HeapSort 优先级队列
堆排序的是集合了插入排序的单数组操作,又有归并排序的时间复杂度,完美的结合了2者的优点. 堆的定义 n个元素的序列{k1,k2,…,kn}当且仅当满足下列关系之一时,称之为堆. 情形1:ki < ...
- VS2008编译boost库
一.下载首先从boost官方主页http://www.boost.org/下载最新版boost安装包,我用的版本是boost.1.49.0二.新建文件夹 如果是使用下载的安装包,那么请将boost安装 ...
- haproxy hdr_beg 配置
v-dev-app01:/root# ping www.zjdev.com PING www.zjdev.com (192.168.32.16) 56(84) bytes of data. 64 by ...
- C++ 文件的复制、删除、重命名
一.文件的复制 #include <iostream>#include <fstream>using namespace std; int CopyFile(char *Sou ...
- Coursera-Neural Networks by Geoffrey Hinton
feed-forward networks symmetrically-connection neural networks
- [11-2] adaboost理解
以二分类问题为例({-1,+1}) adaboost步骤: 1.初始化u1=(1/N,1/N,-,1/N) 2.找到h,使最小化,记该h为g:计算作为该g的权重 3.更新ui: 4.重复2,3得到T个 ...
- 微信公众帐号开发。大家是用框架还是自己写的流程。现在遇到若干问题。请教各路大仙 - V2EX
微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙 - V2EX 微信公众帐号开发.大家是用框架还是自己写的流程.现在遇到若干问题.请教各路大仙
- (Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 ppi 2GB/32GB 14.0Mp camera-in Mobile Phones from Electronics on Aliexpress.com
(Pre sell) ZOPO ZP998 (C2 II) 5.5 inch smart phone True Octa Core MTK6592 1920X1080 FHD screen 401 p ...