一、内容概述

举例说明ViewStub标签的使用

二、ViewStub类的文档说明及应用场举例

文档描述:

A ViewStub is an invisible, zero-sized View that can be used to lazily inflate layout resources at runtime. When a ViewStub is made visible, or when inflate() is invoked, the layout resource is inflated. The ViewStub then replaces itself in its parent with the inflated View or Views. Therefore, the ViewStub exists in the view hierarchy until setVisibility(int) or inflate() is invoked. The inflated View is added to the ViewStub's parent with the ViewStub's layout parameters. Similarly, you can define/override the inflate View's id by using the ViewStub's inflatedId property. For instance: 
  <ViewStub android:id="@+id/stub"             
android:inflatedId="@+id/subTree"
android:layout="@layout/mySubTree"
android:layout_width="120dip"
android:layout_height="40dip" />
The ViewStub thus defined can be found using the id "stub." After inflation of the layout resource "mySubTree," the ViewStub is removed from its parent. The View created by inflating the layout resource "mySubTree" can be found using the id "subTree," specified by the inflatedId property. The inflated View is finally assigned a width of 120dip and a height of 40dip. The preferred way to perform the inflation of the layout resource is the following: 
 ViewStub stub = (ViewStub) findViewById(R.id.stub);    
View inflated = stub.inflate();
When inflate() is invoked, the ViewStub is replaced by the inflated View and the inflated View is returned. This lets applications get a reference to the inflated View without executing an extra findViewById().

上面的问题大致意思是:

ViewStub 是一种不可见且不占屏幕大小的View,它可以用于运行时延迟加载一个布局文件,当它的visibility被设置为View.visiable或者它的inflate方法被调用时
它所引用的布局文件就会被加载,接着ViewStub会用它加载的那个布局文件的View来替代自己。

在上面的例子中,我们可以用id为stub来findViewById,而当我们调用ViewStub.inflate()或者ViewStub.setVisiable(View.VISIABLE)时,那么它的将变成一个由mySubTree布局文件指定的View,而我们可以使用id为subTree去索引这个View。

应用举例:

如下是应用的布局框架,中间的那个FrameLayout正式我们当前Activity的布局,而上面就有一个ViewStub,具体它是系统用来干嘛呢,可以进一步查找。

三、我的demo

1. 定义一个用ViewStub延迟加载的布局文件,这里简单定义如下(headbar.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" > <Button
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="back" >
</Button> <Button
android:id="@+id/go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="go" >
</Button> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/go"
android:layout_toRightOf="@id/back"
android:text="as you see" >
</TextView> </RelativeLayout>

2.mainActivity的布局文件(activity_main.xml)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.stubview.MainActivity" > <ViewStub
android:id="@+id/hiddenHead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inflatedId="@+id/headbar"
android:layout="@layout/headbar" >
</ViewStub> <Button
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="加载View" /> </RelativeLayout>

3.mainActivity中的使用ViewStub 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.show).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ViewStub stubView = (ViewStub) findViewById(R.id.hiddenHead);
View view = stubView.inflate();
Toast.makeText(getApplicationContext(),
"hiddenHead View 的id为" + view.getId(),
Toast.LENGTH_LONG).show();
}
});
}

附代码参考https://github.com/LuLei2013/StubView.git

ViewStub 的使用的更多相关文章

  1. ViewStub的使用

    ViewStub是一个不可见的.大小为0的控件,运行时ViewStub可以滞后加载.当ViewStub置为可见或者调用inflate()的时候,布局就会加载出来.用加载进来的布局取代ViewStub在 ...

  2. ViewStub源码分析

    ViewStub是一种特殊的View,Android官方给出的解释是:一种不可见的(GONE).size是0的占位view,多用于运行时 延迟加载的,也就是说真正需要某个view的时候.在实际项目中, ...

  3. include、merge 、ViewStub

    在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...

  4. Android实战技巧:ViewStub的应用

    在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局.那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在 ...

  5. ViewStub的简单用法和说明

    最近无意间知道了ViewStub,所以特地的去了解了一下 都知道ViewStub是一个不可见的,大小为0的View,实际上跟include差不多,但是ViewStub要更加节约资源.被称为是" ...

  6. Android引导指示层的制作 (ViewStub + SharePreference)

    引导指示界面是个什么鬼东西?一张图即明了:

  7. 【转】Android布局优化之ViewStub

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  8. Android性能优化之一:ViewStub

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

  9. Android优化——UI优化(三)使用ViewStub延迟加载

    使用ViewStub延迟加载 1.ViewStub延迟加载 ViewStub是一个不可见的,大小为0的View,最佳用途就是实现View的延迟加载,在需要的时候再加载View,可Java中常见的性能优 ...

  10. ViewStub的简单解析和使用场景

    ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Android开发人员基本上都听说过,但是真正用的可能不多. ViewStub可以理解成一个非常轻量级的Vie ...

随机推荐

  1. 基于opencv的摄像头的标定

    四个坐标系分别为:世界坐标系(Ow),摄像机坐标系(Oc),图像物理坐标系(O1,单位mm),图像像素坐标系(O,位于视野平面的左上角,单位pix). 空间某点P到其像点p的坐标转换过程主要是通过这四 ...

  2. CF 276C Little Girl and Maximum Sum【贪心+差分】

    C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...

  3. 洛谷 P2415 集合求和【数学公式/模拟】

    给定一个集合s(集合元素数量<=30),求出此集合所有子集元素之和. 输入输出格式 输入格式: 集合中的元素(元素<=1000) 输出格式: 和 输入输出样例 输入样例#1: 2 3 输出 ...

  4. 10、Django实战第10天:找回密码

    今天完成的功能是:用户忘记密码后,通过注册邮箱重置密码... 首先还是把前端页面准备好,把forgetpwd.html复制到templates目录下 编辑users.views.py,创建一个忘记密码 ...

  5. cogs 2554. [福利]可持久化线段树

    题目链接 cogs 2554. [福利]可持久化线段树 题解 没有 代码 #include<cstdio> #include<cstring> #include<algo ...

  6. 【分块】bzoj3196 Tyvj 1730 二逼平衡树

    分块 或 树套树. 在每个块中维护一个有序表,查询时各种二分,全都是分块的经典操作,就不详细说了. 块的大小定为sqrt(n*log2(n))比较快. #include<cstdio> # ...

  7. Eclipse的workspace中放入Ext JS卡死或OOM的解决方案

    Eclipse的workspace中放入Ext JS卡死或OOM的解决方案 原因:是由于Ext JS 的所有的文件js验证 方法一:关于Eclipse解决Ext JS卡死方案: 打开Eclipse的w ...

  8. 【重装系统】老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

    老毛桃U盘工具V2013超级装机版-程序下载和运行 老毛桃U盘工具V2013超级装机版-安装原版XP的方法 老毛桃U盘工具V2013超级装机版-安装原版Win7/Win8

  9. Java - Struts框架教程 Hibernate框架教程 Spring框架入门教程(新版) sping mvc spring boot spring cloud Mybatis

    https://www.zhihu.com/question/21142149 http://how2j.cn/k/hibernate/hibernate-tutorial/31.html?tid=6 ...

  10. Node.js 调用 restful webservice

    如何构建一个restful web service参考原来的文章 http://www.cnblogs.com/ericnie/p/5212748.html 直接用原来的项目编译好像有问题,此处耗费1 ...