ViewStub控件是一个不可见,0尺寸得惰性控件。当ViewStub控件设置可见,或者调用inflate(),并运行完毕之后,ViewStub所指定的layout资源就会被载入。这个ViewStub就会被新载入的layout文件取代。

ViewStub也会从其父控件中移除。

因此ViewStub存在view
Hierarchy中的生存期为直到ViewStub运行了setVisable(int)或者inflate()方法。

在setVisable或者inflate方法运行完成。被载入的Layout文件伴随着ViewStub的属性设置一起被增加ViewStub的父控件中。

你能够定义或者重写被载入layout文件的id属性。

比如:

<ViewStub android:id="@+id/stub"
               android:inflatedId="@+id/subTree"
               android:layout="@layout/mySubTree"
               android:layout_width="120dip"
               android:layout_height="40dip" />

这个定义的ViewStub控件能够通过设定的id“stub”找到。在成功获取subTree制定的kayout之后,ViewStub就从其父控件中移除了,被载入的mySubTree,能够通过id“subTree”找到。从以上的代码属性设置来说,终于载入的Layout文件,会以宽120dp,高40dp展示。

通过下面方式,能够使ViewStub展示出其所代表的layout文件。

   ViewStub stub = (ViewStub) findViewById(R.id.stub);
     View inflated = stub.inflate();
 

当inflate()运行完毕,Viewstub就被替换掉了,inflate返回的View就是mySubTree这个布局文件的View。所以就不须要运行额外的findById了。

值得注意的是:ViewStub在Api1的时候就已经提供了,可是在api16的时候,添加了一个方法:

public void setLayoutInflater (LayoutInflater inflater)

这种方法攻克了,不能指定LayoutInflater 的问题,这涉及了Context的使用。

假设你在低于Api16的开发中,遇到找到不你载入View 的 id的情况,那多数情况是。Context出了问题。在低于api16的情况下,解决方案有两种:

(1)自己定义ViewStub

(2)在V7包中。使用ViewStubCompat类。

作者有话说:假设你须要或者Android中文API。请扫一扫以下的二维码,您的关注。就是我的动力。


Android中文API-ViewStub的更多相关文章

  1. Android 中文 API (40) —— RatingBar

    Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...

  2. Android 中文API (70) —— BluetoothDevice[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothDevice,为Android蓝牙部分的章节翻译.蓝牙设备类,代表了蓝牙通讯国足中的远端设备.版本为 Android  2.3 ...

  3. Android 中文API (69) —— BluetoothAdapter[蓝牙]

    前言 本章内容是  android.bluetooth.BluetoothAdapter,为Android蓝牙部分的章节翻译.本地蓝牙设备的适配类,所有的蓝牙操作都要通过该类完成.版本为 Androi ...

  4. Android 中文API (68) —— BluetoothClass.Service

    前言 本章内容是 android.bluetooth.BluetoothClass.Service,为Android蓝牙部分的章节翻译,版本为 Android 2.3   r1,翻译来自中山大学的&q ...

  5. Android 中文API (67) —— BluetoothClass.Device.Major

    前言 本章内容是android.bluetooth.BluetoothClass.Device.Major,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学 ...

  6. Android 中文API (66) —— BluetoothClass.Device

    前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学的" ...

  7. Android 中文API (65) —— BluetoothClass[蓝牙]

    前言 本章内容是android.bluetooth.BluetoothClass,为Android蓝牙部分的章节翻译.用于描述远端设备的类型,特点等信息,通过getBluetoothClass()方法 ...

  8. android中文api(79)——Gallery

    前言 本章内容是 android.widget.Gallery,版本为Android 2.3 r1,翻译来自"henly.zhang",欢迎大家访问他的博客:http://www. ...

  9. Android中文API

    Android中文API http://www.android-doc.com/index.html

  10. Android 中文 API (29) —— CompoundButton

    前言 本章内容是android.widget.CompoundButton,翻译来自德罗德,再次感谢德罗德 !期待你一起参与Android API 的中文翻译,联系我over140@gmail.com ...

随机推荐

  1. 自己定义控件三部曲之动画篇(十三)——实现ListView Item进入动画

    前言:宝剑锋从磨砺出,梅花香自苦寒来 相关文章: <Android自己定义控件三部曲文章索引>: http://blog.csdn.net/harvic880925/article/det ...

  2. Redis通过命令行进行配置

    redis 127.0.0.1:6379[1]> config set requirepass my_redis OK redis 127.0.0.1:6379[1]> config ge ...

  3. Swift-UITextField用法

    文本框的创建,如下几种方式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRect: ...

  4. Java-MyBatis:MyBatis3 | 日志

    ylbtech-Java-MyBatis:MyBatis3 | 日志 1.返回顶部 1. 日志 Mybatis 的内置日志工厂提供日志功能,内置日志工厂将日志交给以下其中一种工具作代理: SLF4J ...

  5. c++面向对象程序设计 谭浩强 第三章答案

    2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...

  6. layer.js漂亮的弹出框

    它的官方网站:http://layer.layui.com/ 消息.确认框.ifame.自定义文本.旋转木马,都有按钮,是一款强大的js 弹出框: 以下为本人的简单介绍: layer.open({ t ...

  7. 通过修改路由,或者增加Route属性来控制访问webApi的路径

    可以通过RouteConfig.cs文件中的路由规则来控制 通过为每个方法增加单独的[Route(“api/xx类/xx方法”)]

  8. 微信小程序-最新获取用户基本信息方案

    如果只是单纯的展示用户信息,那么最简单的方案就是 文档中组件: <open-data type="groupName" open-gid="xxxxxx" ...

  9. javascript中封装scoll()方法

    function scroll() { var scrollTop = window.pageYOffset || document.documentElement.scrollTop || docu ...

  10. 初学C#,总结一下.sln和.csproj的区别

    1.sln:solusion 解决方案 csproj:c sharp project C#项目 csproj文件大家应该不会陌生,那就是C#项目文件的扩展名,它是“C Sharp Project”的缩 ...