博客
关于我
Android-LayoutInflater.inflate
阅读量:234 次
发布时间:2019-02-28

本文共 2096 字,大约阅读时间需要 6 分钟。

获得 LayoutInflater 实例的三种方式

//获得 LayoutInflater 实例的三种方式://三种方式本质是相同的,最终本质是都是调用的Context.getSystemService()。        LayoutInflater inflater1 = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        LayoutInflater inflater2 = LayoutInflater.from(context);//Fragment可用        LayoutInflater inflater3 = getLayoutInflater();//Activity类下的方法

inflate方法参数介绍:

从android官方API中我们可以看到LayoutInflater共有四个inflate方法,最终都会调用

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

inflate()的作用:就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。

第一个参数resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。

第二个参数root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,

如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。即:true作为根对象,false仅提供LayoutParams。

第三个参数attachToRoot:是否将child添加到root上

1、如果root非空,并且attachToRoot为true的话,则会将当前子视图添加到父视图中;

attachToRoot为true 等同于 root.addView(child);

if (root != null && attachToRoot) {          root.addView(temp, params);}

如:

View childView = getLayoutInflater().inflate(R.layout.child, parentView);//有parentView,则第三个参数默认true/**或**/View childView = getLayoutInflater().inflate(R.layout.child, parentView, true);

2、如果root为空或者attachToRoot为false的话,则直接返回我们创建的temp.

if (root == null || !attachToRoot) {           result = temp;}

如:

View childView = getLayoutInflater().inflate(R.layout.child, null);/**或**/View childView = getLayoutInflater().inflate(R.layout.child, parentView,false);

attachToRoot为false的疑惑:

如果root不为null,而attachToRoot为false的话,表示不将第一个参数所指定的View添加到root中,有什么用呢?既然不添加到root中,那我第二个参数直接给null不就可以了?

其实不然,这里涉及到另外一个问题:我们在开发的过程中给控件所指定的layout_width和layout_height到底是什么意思?该属性的表示一个控件在容器中的大小,就是说这个控件必须在容器中,这个属性才有意义,否则无意义。
这就意味着如果我直接将child加载进来而不给它指定一个父布局,则inflate布局的根节点的layout_width和layout_height属性将会失效(因为这个时候child将不处于任何容器中,那么它的根节点的宽高自然会失效)。如果我想让child的根节点有效,又不想让其处于某一个容器中,那我就可以设置root不为null,而attachToRoot为false。这样,指定root的目的也就很明确了,即root会协助child的根节点生成布局参数,使layout_width和layout_height属性生效,只有这一个作用。

参考:

转载地址:http://mjss.baihongyu.com/

你可能感兴趣的文章
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>
MYSQL CONCAT函数
查看>>
multiprocessing.Pool:map_async 和 imap 有什么区别?
查看>>
MySQL Connector/Net 句柄泄露
查看>>
multiprocessor(中)
查看>>
mysql CPU使用率过高的一次处理经历
查看>>
Multisim中555定时器使用技巧
查看>>
MySQL CRUD 数据表基础操作实战
查看>>
multisim变压器反馈式_穿过隔离栅供电:认识隔离式直流/ 直流偏置电源
查看>>
mysql csv import meets charset
查看>>