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方法有以下两类重载

  1. public View inflate (int resource, ViewGroup root)
  2. 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这篇

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 新买的饮水机热水口出水小怎么办 新买的饮水机热水口不出水怎么办 平胸没有适合自己的内衣怎么办 售楼小姐穿坏的丝袜都怎么办了 蛋白粉一天喝十克补不起来怎么办 跑步时没有卸妆毛孔堵塞了怎么办 自己做的葡萄酒太甜了怎么办 健身馆碰到一个帅的健身教练怎么办 随着年龄的增长脖子越来越短怎么办 安装软件时解析包出现问题怎么办 鼻子吸进去的气往嘴巴里怎么办 做完瑜伽之后大腿后侧特别紧怎么办 刚下生小狗腿后腿站不起来怎么办 脚被凳子压到了流血了怎么办还很痛 小狗脚被凳子压出血了怎么办 怀孕8个月脐带绕颈一周怎么办 练完瑜伽大腿两侧肌肉麻木怎么办 突然吃了辣火锅肚子烫怎么办 副鼻窦炎鼻头顶痛鼻子臭怎么办 一岁宝宝夜里睡觉不踏实怎么办 分手一个月了还是放不下前任怎么办 当晚上遇到烦心事睡不着该怎么办 为什么白天太累晚上就睡不着怎么办 白天走累的脚痛晚上睡不着怎么办 1岁宝宝感冒咳嗽流泪流鼻涕怎么办 宝宝不含母乳只吃奶瓶怎么办 宝宝习惯奶嘴不咬妈妈的乳头怎么办 我的乳晕和私处色素沉着怎么办 双盘打坐膝痛腿脚心处紫青怎么办? 双盘打坐两膝盖不能着地怎么办? 做完蹲起之后腿疼的受不了怎么办 孩子游泳学不会不想去了怎么办 自由泳打腿走水但水花大怎么办 老师上课发现学生课上自慰怎么办 生完孩子腿粗屁股大怎么办 产后42天检查子宫偏大怎么办 崇拜瑜伽老师喜欢上他了怎么办 练瑜伽下不了腰贴不住腿怎么办 吃母乳宝宝吸的奶头疼怎么办 孩子吃奶把奶头咬破了怎么办 奶头边上让孩子咬破了吃奶疼怎么办