tion(" must be the root element");
? ? ? ? ? ? } else if (TAG_1995.equals(name)) {
? ? ? ? ? ? ? ? final View view = new BlinkLayout(mContext, attrs);
? ? ? ? ? ? ? ? final ViewGroup viewGroup = (ViewGroup) parent;
? ? ? ? ? ? ? ? final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
? ? ? ? ? ? ? ? rInflate(parser, view, attrs, true);
? ? ? ? ? ? ? ? viewGroup.addView(view, params);? ? ? ? ? ? ? ?
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? final View view = createViewFromTag(parent, name, attrs);
? ? ? ? ? ? ? ? final ViewGroup viewGroup = (ViewGroup) parent;
? ? ? ? ? ? ? ? final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
? ? ? ? ? ? ? ? rInflate(parser, view, attrs, true);
? ? ? ? ? ? ? ? viewGroup.addView(view, params);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (finishInflate) parent.onFinishInflate();
? ? }
里面也用到onCreateView方法创建子view,然后将其加入到父view中返回。
通过查看上面的源码,我们可以发现inflate方法中的三个参数int resource, ViewGroup root, boolean attachToRoot的作用如下:
resource指定了要加载的view,root作为view外面一层的父容器,attachToRoot表示是否将view加入到父容器。
当指定了父容器,并且attachToRoot为true,则将view加入到父容器中。
如果指定了父容器,却将attachToRoot设置为false,那么只是从父容器中生成了view布局的参数并设置给view
当未指定父容器时,直接返回view本身。
总结
通过研究LayoutInflater源码的设计,我们了解到代码的执行细节的同时,也可以发现:
LayoutInflater创建view对象时候使用了简单工厂模式,并通过加入钩子方法,利用抽象工厂模式让coder可以使用自定义的工厂方法来创建view。