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. Cocos2d-x学习资源集锦+有奖抽楼活动

    大家好,事实上我是这个游戏开发版块的新任版主之中的一个,可能大家的焦点都在candycat1992女版主身上,所以我认为我应该冒个泡. 俗话说,新版主上任,三把"水"(是你自己说的 ...

  2. Light OJ 1288 Subsets Forming Perfect Squares 高斯消元求矩阵的秩

    题目来源:Light OJ 1288 Subsets Forming Perfect Squares 题意:给你n个数 选出一些数 他们的乘积是全然平方数 求有多少种方案 思路:每一个数分解因子 每隔 ...

  3. Common webpart properties in kentico

    https://devnet.kentico.com/docs/7_0/devguide/index.html?common_web_part_properties.htm HTML Envelope ...

  4. MVC/MVP/MVVM区别——MVVM就是angular,视图和数据双向绑定

    摘自:http://www.ruanyifeng.com/blog/2015/02/mvcmvp_mvvm.html 一.MVC MVC模式的意思是,软件可以分成三个部分. 视图(View):用户界面 ...

  5. DB-MySQL:MySQL NULL 值处理

    ylbtech-DB-MySQL:MySQL NULL 值处理 1.返回顶部 1. MySQL NULL 值处理 我们已经知道 MySQL 使用 SQL SELECT 命令及 WHERE 子句来读取数 ...

  6. 基本类型转换成NSNumber类型

    int i=100; float f=2.34; NSNumber *n1=[NSNumber numberWithInt:i]; NSNumber *n2=[NSNumber numberWithF ...

  7. BZOJ 4516 后缀数组+ST+set

    写了一半 没了啊啊啊 重新写的 思路: 先不考虑后缀自动机 (我不会啊) 那这道题只能用后缀数组了 先把原串倒一下 后缀->前缀 相当于每回在前面加了一个字母 求不同的子串个数 首先 正常的求子 ...

  8. 【转】sql 一对多情况下 Group by分组 结果多列合并

    部分原始表数据 需求: 按routineId进行group分组 初步想法(错误): select r * from autowork.dbo.PartOnRoutine where routineId ...

  9. Android ListView getView()方法重复调用导致position错位

    问题现状:Android ListView getView()方法重复调用导致position错位 解决办法:把ListView布局文件的layout_height属性改为fill_parent或者m ...

  10. 从EntityFramework转换EntityFrameworkCore的正确姿势(DBFirst)

    今天有一个小任务:要把一个数据的数据搬运到另一个数据库(两个数据库的数据结构很不一样). 决定用.net core  console app来跑,并且采用entityframework 去做数据CRU ...