Android之LayoutInflater
来源:互联网 发布:三层海鲜淘宝店叫啥 编辑:程序博客网 时间:2024/05/20 00:51
LayoutInflater。Layout 布局,inflater,充气,填充,这个类用于将XML文件转换成相对应的ViewGroup和控件Widget等View。官方的解释为
- Instantiates a layout XML file into its corresponding View objects. It is never used directly. Instead, use getLayoutInflater() or getSystemService(Class) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:
LayoutInflater inflater = (LayoutInflater)context.getSystemService (Context.LAYOUT_INFLATER_SERVICE);
第一句翻译过来就是将layout布局文件实例化成一个View。在这里就和View类中的findViewById(R.id.xxxx)比较类似。事实上,在LayoutInflater类中也有类似的方法来完成这项工作:inflater.inflate()方法,下面对该方法进行具体的分析。
常用的inflate方法有以下两类重载
- public View inflate (int resource, ViewGroup root)
- public View inflate (int resource, ViewGroup root, boolean attachToRoot)
对于第一个方法,也就是把id为resource(R.layout.xxxx)的xml文件实例化成View,如果root不为空,则表示root为实例View的父容器,否则实例View本身作为父容器, 这点比较好理解。再看看返回值:
- The root View of the inflated hierarchy. If root was supplied, this
is the root View; otherwise it is the root of the inflated XML file.
最后返回的都是root view,也就是父容器
举个自定义栗子:
public class MyCustomView extends LinearLayout { ... private void init() { LayoutInflater inflater = LayoutInflater.from(getContext()); inflater.inflate(R.layout.view_with_merge_tag, this); }}
这里的GroupView其实就是LinearLayout
对于第二个方法,多了一个attachToRoot参数。官方文档上的解释为:
- boolean: Whether the inflated hierarchy should be attached to the
root parameter? If false, root is only used to create the correct
subclass of LayoutParams for the root view in the XML.
被填充的层是否应该和root关联?如果是false,root参数只用作为XML根元素View创建正确的LayoutParams的子类,View本身作为父容器,(否则和上面的方法一样)
其实如果上面的难以理解,还能从返回值的定义来理解:
- The root View of the inflated hierarchy. If root was supplied and
attachToRoot is true, this is root; otherwise it is the root of the
inflated XML file.
返回root View,如果root不为空 而且 attachToRoot为true,则root为父容器返回,否则(其他情况)View本身作为父容器。
下面分别对attachToRoot参数进行分析
attachToRoot为True而且root存在时
在xml文件中一个组件的布局代码如下:
<Button xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/custom_button"></Button>
把这个布局动态地添加到Fragment的的LinearLayout中,如果这里LinearLayout已经是一个成员变量mLinearLayout了,调用如下代码
inflater.inflate(R.layout.custom_button, mLinearLayout, true);
我们指定了用于填充button的layout资源文件,然后我们告诉LayoutInflater我们想把button添加到mLinearLayout中。这里Button的LayoutParams种类为LinearLayout.LayoutParams。
attachToRoot为False
我们看一下什么时候attachToRoot应该是false。在这种情况下,inflate()方法中的第一个参数所指定的View不会被添加到第二个参数所指定的ViewGroup中。
回忆一下刚才的例子中的Button,我们想通过layout文件添加自定义的Button至mLinearLayout中。当attachToRoot为false时,我们仍可以将Button添加到mLinearLayout中,但是这需要我们自己动手。
Button button = (Button) inflater.inflate(R.layout.custom_button, mLinearLayout, false);mLinearLayout.addView(button);
这段代码和前一种情况等价
用源码中的一段代码来解释,你就会豁然开朗。所有inflate()方法最后都会调用这个方法:
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) { ... if (TAG_MERGE.equals(name)) { ... rInflate(parser, root, attrs); } else { View temp = createViewFromTag(name, attrs); ViewGroup.LayoutParams params = null; if (root != null) { params = root.generateLayoutParams(attrs); if (!attachToRoot) { temp.setLayoutParams(params); } } rInflate(parser, temp, attrs); if (root != null && attachToRoot) { root.addView(temp, params); } if (root == null || !attachToRoot) { result = temp; } } } catch (XmlPullParserException e) { ... } catch (IOException e) { ... } return result; } }
最后都是对root和attachToRoot的判断有木有!!!其中的createViewFromTag就是获取View实例,rInflate是个递归方法,不断遍历根布局(由createViewFromTag获取到,也就是源码中的temp)下的子View。
未完待续。。。
参考伯乐在线这篇还有CSDN这篇
- android开发之LayoutInflater
- Android之LayoutInflater
- android开发之LayoutInflater
- Android 之LayoutInflater详解
- Android 之 LayoutInflater
- Android之LayoutInflater详解
- Android之LayoutInflater
- android之LayoutInflater
- Android之LayoutInflater探索
- Android笔记之LayoutInflater
- Android 之LayoutInflater.from
- android之LayoutInflater详解_什么是LayoutInflater
- Android之setContentView和LayoutInflater
- Android之setContentView和LayoutInflater
- Android之setContentView和LayoutInflater
- Android之LayoutInflater的使用
- Android之setContentView和LayoutInflater
- Android布局学习之LayoutInflater
- [矩阵快速幂] LightOJ 1142 - Summing up Powers (II)
- -bash: rz: command not found
- 设计模式C++生成器模式
- hdu1434(优先队列&&模拟)
- c++primer(第五版) 第十四章 重载运算与类型转换习题答案
- Android之LayoutInflater
- 往手机插入联系人
- 倒排文件索引结构和搜索算法
- 在Action中访问WEB资源
- Android 双击back键退出应用的两种方式
- CentOS6.5 ping: unknown host 解决方法
- Bitmap和BitmapFactory对象使用
- 在全球经济持续低迷情况下,看唯品会如何逆袭
- 获取程序执行时间的两种方法