LayoutInflater解析 (api28)

  • LayoutInflater.inflate(resId, root) 实现将xml文件解析为view对象(形成一个DOM树结构)
  • view.findViewById(resId) 在view树结构中寻找id为resId的view,并返回

LayoutInflater实例化

LayoutInflater实例化有三种方式,方式一、方式二、最终都是通过方式三来实例化的

//方式一,调用Activity的getLayoutInflater()
LayoutInflater inflater = getLayoutInflater();  
//方式二 from
LayoutInflater inflater = LayoutInflater.from(this);
//方式三
LayoutInflater inflater =  (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
方式一:getLayoutInflater()
  • activity.getLayoutInflater() -> phoneWindow.getLayoutInflater() ->返回一个mLayoutInflater
  • mLayoutInflater是在PhoneWindow实例化时创建的,PhoneWindow是在Activity.attach()时实例化的

也就是说创建activity时,就绑定了一个PhoneWindow对象,并通过LayoutInflater.from(context) 得到了一个LayoutInflater对象

//Activity.java
public LayoutInflater getLayoutInflater() {
    return getWindow().getLayoutInflater();
}
//PhoneWindow.java
private LayoutInflater mLayoutInflater;
 
 public PhoneWindow(Context context) {
    super(context);
    mLayoutInflater = LayoutInflater.from(context);
}

@Override
public LayoutInflater getLayoutInflater() {
    return mLayoutInflater;
}
方式二:LayoutInflater.from(context)

方式二是通过方式三得到一个LayoutInflater对象

//LayoutInflater.java
 public static LayoutInflater from(Context context) {
        LayoutInflater LayoutInflater =
                (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (LayoutInflater == null) {
            throw new AssertionError("LayoutInflater not found.");
        }
        return LayoutInflater;
    }

LayoutInflater加载xml布局

  • 方式一:返回layout_x的rootView,view.layoutParams = null,layout_x的布局参数信息失效
  • 方式二:返回layout_x的rootView,view.layoutParams != null,layout_x的布局参数信息生效
  • 方式三:返回root,view.layoutParams != null,layout_x的布局参数信息生效

(因为如果view.layoutParams == null,则会根据root生成一个默认的layoutParams。)

//方式一,需要重新设置view的layoutParams参数,layout_x的布局参数信息失效
view = LayoutInflater.from(context).inflate(R.layout.layout_x, null)
root.addView(view)
//方式二,layout_x的布局参数信息生效
view = LayoutInflater.from(context).inflate(R.layout.layout_x, root, false)
root.addView(view)
//方式三,layout_x的布局参数信息生效,内部实现了root.addView(view)
view = LayoutInflater.from(context).inflate(R.layout.layout_x, root, true)
LayoutInflater.inflate(resId, root, attach)
//LayoutInflater.java
	public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }
    
    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        final XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }
	
	public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            final Context inflaterContext = mContext;
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            View result = root;
            try {
                // Look for the root node. 寻找root节点
                final String name = parser.getName();
                    // Temp is the root view that was found in the xml
                    final View temp = createViewFromTag(root, name, inflaterContext, attrs);
                    ViewGroup.LayoutParams params = null;
                    if (root != null) {
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        //如果root为null,都不会解析出view的layoutParams,xml中设置的顶布局参数失效
                        //如果root不为null,attachToRoot为false,给view设置LayoutParams
                        //否则root不为null,attachToRoot为true,则调用add(view,params)
                        if (!attachToRoot) { 
                            temp.setLayoutParams(params);
                        }
                    }
                    // Inflate all children under temp against its context.
                    // 遍历创建temp下的所有节点view
                    rInflateChildren(parser, temp, attrs, true);
					//root不为null,attachToRoot为true,则调用add(view,params)
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }
                    // 如果root不为null,且attachToRoot为true,返回的是root
                    //否则返回的是layout_x的顶布局temp
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }
                }
            } 
            return result;
        }
    }
ViewGroup.addView

会添加view到viewGroup中,并申请重绘布局,展示childView

//ViewGroup.java
	public void addView(View child) {
        addView(child, -1); // -1 add last,添加在最后
    }
    
    public void addView(View child, int index) {
        LayoutParams params = child.getLayoutParams();
        if (params == null) {//手动补上LayoutParams信息,子类可能会重写generateDefaultLayoutParams()
            params = generateDefaultLayoutParams(); 
        }
        addView(child, index, params);
    }
    
	//子类可能会重写generateDefaultLayoutParams()
	protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }
	
	public void addView(View child, int index, LayoutParams params) {
        // addViewInner() will call child.requestLayout() when setting the new LayoutParams
        // therefore, we call requestLayout() on ourselves before, so that the child's request
        // will be blocked at our level
        requestLayout();
        invalidate(true);
        addViewInner(child, index, params, false); //里面会绑定child和params的关系
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值