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. C#趣味程序---三色球问题

    问题:若一个口袋中放有12个球,3红3白和6黑,问从袋中随意取8个球,有多少种不同的颜色搭配? using System; namespace ConsoleApplication1 { class ...

  2. UITableView属性 自己定义UITableViewCell

    UITableView的属性全齐.供大家參考 附:http://www.bubuko.com/infodetail-561085.html //曾经在使用UITableView的时候,总是在cell上 ...

  3. 谷歌开源可视化工具Facets,将用于人+AI协作项目研究——无非就是一个用于特征工程探索的绘图工具集,pandas可以做的

    见:http://www.infoq.com/cn/news/2017/07/goole-sight-facets-ai https://github.com/PAIR-code/facets/blo ...

  4. hdoj--3062--party(2-sat 可行解)

    Party Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  5. 【转】小白级的CocoaPods安装和使用教程

    原文网址:http://www.jianshu.com/p/e2f65848dddc 百度有很多CocoaPods的安装教程.第一次看的时候,确实有点摸不透的感觉.经过思考,一步一步来实践,前后花了三 ...

  6. POJ 3185 DFS

    好像可以用高斯消元??? 但是用搜索写 这题就很水了... // by SiriusRen #include <bitset> #include <cstdio> using ...

  7. java实现sql批量插入参数

    背景: 需要更新一些不规范的时间格式,如将某个时间格式化为yy-MM-dd,实际上为 yy-MM-dd hh:mm:ss,并且需要提供回滚脚本. 例如:规范化时间的脚本如下: ,) WHERE tes ...

  8. SQL Server 获取两个日期间的日期

    declare @start datetime declare @end datetime set @start = '2018-01-25' set @end = '2018-02-03' sele ...

  9. Oracle学习系类篇(三)

    1. 存储过程 CREATE  OR REPLACE PROCEDURE  SP_NAME( PM_NAME [IN/OUT/IN OUT] PM_TYPE...)            {AS}   ...

  10. DevExpress Report 打印提示one or more margins are set outside the printable area of the page 问题解决

    DevExpress  Report Print的时候,出现这样的问题:one or more margins are set outside the printable area of the pa ...