Android LayoutInflater.inflater方法详解

一、简介

inflate方法是LayoutInflater类中的一个公有方法,该方法共有4个重载的方法原型,不过看源码便知,如下方法1,2,3最后兜兜转转都调用了方法4:

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

public View inflate(XmlPullParser parser, @Nullable ViewGroup root)

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

public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

二、主要方法详解

public View inflate(XmlPullParser parser, @Nullable ViewGroup root,
   boolean attachToRoot) {
  synchronized (mConstructorArgs) {
   final Context inflaterContext = mContext;
   /*
    * attrs:从给定的parser中获得的一个xml属性集,我们一般不会直接使用该类型参数
    * 所以不用管,只需知道attrs中包含了我们传进去的layout文件中所定义的参数属性值便好
    */
   final AttributeSet attrs = Xml.asAttributeSet(parser);
   Context lastContext = (Context) mConstructorArgs[0];
   mConstructorArgs[0] = inflaterContext;
   /* result为待返回的View对象 */
   View result = root;
   /*
    * 这里有一段代码,未贴出来 功能:寻找xml中的根节点,找到时将起始节点的名称赋值给name 若没有找到则抛异常
    */
   if (TAG_MERGE.equals(name)) {
    // 当根节点为merge节点时的处理
    if (root == null || !attachToRoot) {
     throw new InflateException(
       "<merge /> can be used only with a valid "
         + "ViewGroup root and attachToRoot=true");
    }
    /*
     * 这是一个递归方法,从xml根节点开始向下递归,先初始化该层view,
     * 然后初始化下一层view,当整个view树都生成完毕时调用onFinishInflate()
     * 接下来我们就可以通过findViewById方法找到具有id属性的view了
     */
    rInflate(parser, root, inflaterContext, attrs, false);
   } else {
    // 根节点不为merge节点时的处理
    /*
     * temp为xml文件中的根节点view
     * createViewFromTag方法用于根据给定的参数集attrs为名为name的tag标签生成一个view
     * 由上文可知,此处name为xml文件的根节点名称,所以temp为xml的root view
     */
    final View temp = createViewFromTag(root, name,
      inflaterContext, attrs);
    ViewGroup.LayoutParams params = null;
    if (root != null) {
     // 注意:root非空时的处理
     // Create layout params that match root, if supplied
     /*
      * generateLayoutParams是ViewGroup中提供的一个public方法
      * 用于根据给定的参数集attrs生成一个LayoutParams
      * LayoutParams一般由view使用,用于告诉view的parent,他们想要的layout样式
      * 一个基本的LayoutParams一般只用于描述他们的size信息,即view的width和height
      * 
      * 注意:此处通过root来调用generateLayoutParams得到了一个LayoutParams对象
      */
     params = root.generateLayoutParams(attrs);
     if (!attachToRoot) {
      /*
       * 当root != null && attachToRoot == false时(即前面说的Case2)
       * 用params所指代的LayoutParams为tamp设置布局参数
       */
      temp.setLayoutParams(params);
     }
    }
    // Inflate all children under temp against its context.
    rInflateChildren(parser, temp, attrs, true);
    /*
     * 当root != null && attachToRoot == true时(即前面说的Case3)
     * 将根节点为temp的view树添加到root上,temp的布局参数为params
     */
    if (root != null && attachToRoot) {
     root.addView(temp, params);
    }
    /*
     * 当root == null || !attachToRoot时(前面说的Case1和Case2属于这种情况)
     * 将temp赋值给result
     */
    if (root == null || !attachToRoot) {
     result = temp;
    }
   }
   /*
    * 返回得到的view树的根节点
    * 
    * 注意:不同情况下的result值是不一样的
    * 当root == null时,result为xml根节点view,但并未为该view设置LayoutParams值
    * 当root != null && attachToRoot == false时
    * result为xml根节点view,且根据xml文件中提供的参数值为该view设置了LayoutParams值
    * 当root != null && attachToRoot == true时,result为初始值root
    */
   return result;
  }
}

layout_root:

  • 参数:

    • parser:一个xml dom节点,在该节点中包含了待生成的视图层次结构的描述信息

    • root:一个可选的ViewGroup对象,当attachToRoot为true时,root为生成的view树的 parent;当attachToRoot为false时,root仅为一个为生成的视图层次提供参数的对象

    • attachToRoot:是否将生成的视图层次附加到root参数上,false:root仅仅用于为xml文件的根节点创建正确的参数子类

  • 返回:返回生成的view树的根节点

  • 参数组合:(注:参数parser不能为空,若为空,会提示错误):

    • Case1:当参数root == null时,函数调用为inflate(parser, null)

    • Case2:当参数root != null && attachToRoot == false时,函数调用为inflate(parser, root, false)

    • Case3:当参数root != null && attachToRoot == true时,函数调用为inflate(parser, root, true)

那么如上几种参数场景都会有怎样的返回值呢?我们接下来一起看看inflate方法内部的实现吧!

inflate方法源码分析:

参数组合结果:
假设root 的xml文件为layout_root,待infalte的xml文件为layout_child:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_root"
    android:orientation="vertical"
    android:layout_width="300dp"
    android:layout_height="400dp"
    android:background="@color/forestgreen"
>
<TextView
    android:id="@+id/tv_root_ll"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="LL and root"
    android:layout_gravity="center_horizontal"
    android:textSize="20sp"
    />
</LinearLayout>

layout_child:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_child"
    android:orientation="vertical"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:background="@color/violet"
    >
    <TextView
        android:id="@+id/tv_child_ll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="LL and child"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        />
</LinearLayout>

所以由4中的分析可知,3中3种case下面的返回值分别为:

  • Case1:返回id为ll_child的LinearLayout视图,该view的LayoutParams为null

  • Case2:返回id为ll_child的LinearLayout视图,该view的LayoutParams为xml文件layout_child中提供的参数,即width和height分别为200dp和200dp

  • Case3:返回id为ll_root的LinearLayout视图,该view的LayoutParams为xml文件layout_root中提供的参数,即width和height分别为300dp和400dp,同时由于这是一个vertical的LinearLayout,所以会将layout_child所代表的view树添加到id为tv_child_ll的TextView下面

实验:

下面根据上面三种case分别进行实验,查看显示效果

  • Case 1:
 private void caseOne(){
    childView = inflater.inflate(R.layout.layout_child, null);
    setContentView(childView);
}

实验结果:


Case 1

结果分析:由5可知,该情况下返回的是一个LayoutParams == null,根节点id为ll_child的LinearLayout,至于为什么显示效果如上图所示则涉及到setContentView方法以及Android的窗口加载原理,简而言之就是在Android加载布局文件时,如果view的LayoutParams为null,则默认将该view的width和height赋值为MATCH_PARENT,具体请参考我的下一篇博客《Android 从setContentView谈Activity界面的加载过程》

  • Case 2:
private void caseTwo(){
    rootView = (ViewGroup)inflater.inflate(R.layout.layout_root,null);
    childView = inflater.inflate(R.layout.layout_child, rootView, false);
    setContentView(childView);
}

实验结果:

Case 2

结果分析:由上图可以看出,该情况与5中的分析相符
  • Case 3:
private void caseThree(){
    rootView = (ViewGroup)inflater.inflate(R.layout.layout_root,null);
    childView = inflater.inflate(R.layout.layout_child, rootView, true);
    setContentView(childView);
}

实验结果:


Case 3

结果分析:由上图可以看出,该情况与5中的分析相符

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容