xmlからview【Android】へ


方法1:
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
                }
}

方法2: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();
        }
    }
以上の2つの方式を見渡すと、最終的にはLayoutInflaterが呼び出される.java  -- inflate(int resource, ViewGroup root, boolean attachToRoot)
方法