Android LayoutInflater.inflate()源码梳理

Android中LayoutInflater.inflate()的使用频率还是比较高的,不管是动态添加view还是在Recyclerview.Adapter中创建ViewHolder,经常会使用到这个方法,今天对此方法做次梳理,方遍日后查阅。

inflate()及其重载有如下几个:

  1. View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
  2. View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
  3. View inflate(XmlPullParser parser, @Nullable ViewGroup root)
  4. View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)

对于方法2中三个参数:

resource:布局文件id

root:ViewGruop类型的view,跟参数三attachToRoot联动使用

  • 当root为nul时,attachToRoot参数值true或false没有区别,返回view为xml布局文件中根view(即源码中temp变量),其LayoutParams为null。

该情况仅返回xml对应的view对象,没有设置LayoutParams,不做其它处理。

  • 当root不为null,attachToRoot为true时,表示xml文件以root为父控件,该view对应的宽高为xml文件中定义的宽高,其LayoutParams不为null。

该情况以root为结果返回的同时,通过 root.addView(temp, params)将xml添加到了root中,并指定了params参数。

  • 当root不为null,attachToRoot为false时,表示xml文件不以root为父控件,该view的宽高为xml文件中定义的宽高,其LayoutParams不为null。

该情况在返回xml对应的view的同时,通过temp.setLayoutParams(params)设置了params参数,允许其它View通过addView()添加到自己的布局中,并且该view自带params参数。

方法1内部调用2,2内部调用4;3内部调用4,重点看下方法4。

LayoutInflater.java

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

                   ................省略部分代码
                   // Temp is the root view that was found in the xml
                   // 从xml文件中获取根view temp
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                    ViewGroup.LayoutParams params = null;
                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        //从xml文件中获取宽、高、背景色及其它属性
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            //参数三为false,为temp设置params
                            temp.setLayoutParams(params);
                        }
                    }
                    ...............省略部分代码
                     // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    // 参数三为true,将temp添加到root中,其布局参数为params
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    // root为null,不为其设置params,其params为null
                    // root不为null且参数三为false,返回结果为xml的根view
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }

}
复制代码

前面一直在强调,params参数是否为null,这个参数很重要。

当我们通过addView(view)添加view时,view有没有params很重要。

ViewGroup.java

public void addView(View child) {
        addView(child, -1);
}
    public void addView(View child, int index) {
        if (child == null) {
            throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
        }
        LayoutParams params = child.getLayoutParams();
        if (params == null) {
            params = generateDefaultLayoutParams();
            if (params == null) {
                throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
            }
        }
        addView(child, index, params);
  }
复制代码

当child的布局参数LayoutParams为null时,会通过generateDefaultLayoutParams()方法为其创建LayoutParams,不同布局,其generateDefaultLayoutParams()的实现不一样。

  • 线性布局:如果其方向为水平排列,会为view添加宽高均为LayoutParams.WRAP_CONTENT类型的布局参数;如果其方向为竖直,会为view添加宽为LayoutParams.MATCH_PARENT高为LayoutParams.WRAP_CONTENT类型的布局参数。
  • 相对布局:默认为view添加宽高均为LayoutParams.WRAP_CONTENT类型的布局参数。
  • 帧布局:默认为view添加宽高均为LayoutParams.MATCH_PARENT类型的布局参数。
ViewGroup.java
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
    
LinearLayout.java
       protected LayoutParams generateDefaultLayoutParams() {
        if (mOrientation == HORIZONTAL) {
            return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        } else if (mOrientation == VERTICAL) {
            return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        }
        return null;
    }
    
RelativeLayout.java

 protected ViewGroup.LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
    
FrameLayout.java
    
     protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }
复制代码

正是因为不同布局在addView时会对params为null的view添加不同宽高限制的Layoutparams,会导致我们在xml文件中为根view指定的宽高不生效。

  • parent为RelativeLayout时:view的宽高为wrapConent。
  • parent为FramLayout时:view的宽高为帧布局的宽高。
  • parent为LinearLayout:方向为水平,view的宽高为wrapConent;方向为竖直时,view宽为LinearLayout宽度,高为wrapConent。
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享