xml转成view【Android】

方式一:

View.inflate(context, resource, root);

以下是调用步骤:

View.java  --   inflate(Context context, int resource, ViewGroup root)方法

public static View <span style="color:#ff6600;">inflate(Context context, int resource, ViewGroup root)</span> {
        LayoutInflater factory = LayoutInflater.from(context);
        return <span style="color:#ff6600;">factory.inflate(resource, root);</span>
    }

LayoutInflater .java  -- inflate(int resource, ViewGroup root)方法

public View <span style="color:#ff0000;">inflate(int resource, ViewGroup root)</span> {
        return <span style="color:#ff6600;">inflate(resource, root, root != null);</span>
    }

LayoutInflater .java  -- inflate(int resource, ViewGroup root, boolean attachToRoot)方法

public View <span style="color:#ff0000;">inflate(int resource, ViewGroup root, boolean attachToRoot)</span> {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().<span style="color:#ff0000;">getLayout(resource);</span>
        try {
            return <span style="color:#ff0000;">inflate(parser, root, attachToRoot);</span>
        } finally {
            parser.close();
        }
    }

public XmlResourceParser getLayout(int id) throws NotFoundException {
        return loadXmlResourceParser(id, "layout");
    }

LayoutInflater .java  -- inflate(int resource, ViewGroup root, boolean attachToRoot)方法  部分代码:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            final AttributeSet attrs = Xml.asAttributeSet(parser);
            Context lastContext = (Context)mConstructorArgs[0];
            mConstructorArgs[0] = mContext;
            View result = root;

            try {
                // Look for the root node.
                int type;
                while ((type = parser.next()) != XmlPullParser.START_TAG &&
                        type != XmlPullParser.END_DOCUMENT) {
                    // Empty
                }
}


方式二:  Activity  --  setContentView(int layoutResID)

public void setContentView(int layoutResID) {
        getWindow().setContentView(layoutResID);
        initActionBar();
    }

Window -- public abstract void setContentView(int layoutResID);

实现方法:  PhoneWindow  -- public void setContentView(int layoutResID)

public void setContentView(int layoutResID) {
        if (mContentParent == null) {
            installDecor();
        } else {
            mContentParent.removeAllViews();
        }
        mLayoutInflater.inflate(layoutResID, mContentParent);
        final Callback cb = getCallback();
        if (cb != null) {
            cb.onContentChanged();
        }
    }
纵观以上两方式,最终都会调用LayoutInflater .java  -- inflate(int resource, ViewGroup root, boolean attachToRoot) 方法 










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值