Android源码_Activity加载View

0,主体简介

从ActivityThread到开始加载Activity时,如何将View加载出来

1,时序图

2,细节点

2-1),setContentView中的layoutResId是如何加入DecorView的

    public void setContentView(int layoutResID) {
        ...
        if (mContentParent == null) {
            installDecor();
        } 

        if (hasFeature(FEATURE_CONTENT_TRANSITIONS)) {
            ...
        } else {
            mLayoutInflater.inflate(layoutResID, mContentParent);
        }
        ...
    }
	
    private void installDecor() {
        if (mDecor == null) {
            mDecor = generateDecor();
            ...
        }
        if (mContentParent == null) {
            mContentParent = generateLayout(mDecor);
			...
		}
	}
	
    protected DecorView generateDecor() {
        return new DecorView(getContext(), -1);
    }
	
    protected ViewGroup generateLayout(DecorView decor) {
		...
		layoutResource = R.layout.screen_simple;
	
		View in = mLayoutInflater.inflate(layoutResource, null);
        decor.addView(in, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
        mContentRoot = (ViewGroup) in;
		...
	}

            1,DecorView先加载R.layout.screen_simple文件,并得到contentParent的控件
            2,contentParent将layoutResId加载进来

2-2),xml如何被解析并创建出View的

    public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
			...
            rInflate(parser, root, inflaterContext, attrs, false);
			...
        }
    }
	
    void rInflate(XmlPullParser parser, View parent, Context context,
            AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {
		...
        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {
            ...
			final View view = createViewFromTag(parent, name, context, attrs);
			final ViewGroup viewGroup = (ViewGroup) parent;
			final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);
			rInflateChildren(parser, view, attrs, true);
			viewGroup.addView(view, params);
            ...
        }
        ...
    }
	
    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
        ...
        view = createView(name, null, attrs);
        ...
    }
	
    public final View createView(String name, String prefix, AttributeSet attrs)
            throws ClassNotFoundException, InflateException {
        Constructor<? extends View> constructor = sConstructorMap.get(name);
        Class<? extends View> clazz = null;
		...
		clazz = mContext.getClassLoader().loadClass(
				prefix != null ? (prefix + name) : name).asSubclass(View.class);
		...
		constructor = clazz.getConstructor(mConstructorSignature);
		constructor.setAccessible(true);
		sConstructorMap.put(name, constructor);
		...
		final View view = constructor.newInstance(args);
		...
	}

            1,while循环中,解析xml,获取name
            2,依据name,获取clazz,通过clazz.getConstructor().newInstance()创建出View对象
            3,将view添加到当前的parent中

3,总结

       1,Activity到View的加载,DecorView(PhoneWindow)是核心
       2,ViewRootImpl作用是封装一层对decorView的具体操作

 

 

ylineSign

QQ群:644213963

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值