LayoutInflater 解析

来源:互联网 发布:mfc socket编程实例 编辑:程序博客网 时间:2024/06/04 18:40

LayoutInflater 主要是用来加载xml布局,常见于RecyclerView的Adapter中 以及动态加载View的时候。

1、获取实例

获取实例主要有三种方法:

getLayoutInflater();

LayoutInflater.from(context);

(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

其中 第二种方法其实跟第三种是一样的,如果点进去看,就发现第二种在内部还是调用的第三种方法。

/**     * Obtains the LayoutInflater from the given context.     */    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;    }

2、主要方法

layoutInflater.inflate(resource,root);
layoutInflater.inflate(resource,root, attachToRoot);
当然了,前一个方法内部又是调用的后一个方法。
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {        return inflate(resource, root, root != null);    }
而inflate方法中最终执行的代码如下
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {        synchronized (mConstructorArgs) {            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");            final Context inflaterContext = mContext;            final AttributeSet attrs = Xml.asAttributeSet(parser);            Context lastContext = (Context) mConstructorArgs[0];            mConstructorArgs[0] = inflaterContext;            View result = root;            try {                // Look for the root node.                int type;                while ((type = parser.next()) != XmlPullParser.START_TAG &&                        type != XmlPullParser.END_DOCUMENT) {                    // Empty                }                if (type != XmlPullParser.START_TAG) {                    throw new InflateException(parser.getPositionDescription()                            + ": No start tag found!");                }                final String name = parser.getName();                                if (DEBUG) {                    System.out.println("**************************");                    System.out.println("Creating root view: "                            + name);                    System.out.println("**************************");                }                if (TAG_MERGE.equals(name)) {                    if (root == null || !attachToRoot) {                        throw new InflateException("<merge /> can be used only with a valid "                                + "ViewGroup root and attachToRoot=true");                    }                    rInflate(parser, root, inflaterContext, attrs, false);                } else {                    // 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) {                        if (DEBUG) {                            System.out.println("Creating params from root: " +                                    root);                        }                        // Create layout params that match root, if supplied                        params = root.generateLayoutParams(attrs);                        if (!attachToRoot) {                            // Set the layout params for temp if we are not                            // attaching. (If we are, we use addView, below)                            temp.setLayoutParams(params);                        }                    }                    if (DEBUG) {                        System.out.println("-----> start inflating children");                    }                    // Inflate all children under temp against its context.                    rInflateChildren(parser, temp, attrs, true);                    if (DEBUG) {                        System.out.println("-----> done inflating children");                    }                    // We are supposed to attach all the views we found (int temp)                    // to root. Do that now.                    if (root != null && attachToRoot) {                        root.addView(temp, params);                    }                    // Decide whether to return the root that was passed in or the                    // top view found in xml.                    if (root == null || !attachToRoot) {                        result = temp;                    }                }            } catch (XmlPullParserException e) {                final InflateException ie = new InflateException(e.getMessage(), e);                ie.setStackTrace(EMPTY_STACK_TRACE);                throw ie;            } catch (Exception e) {                final InflateException ie = new InflateException(parser.getPositionDescription()                        + ": " + e.getMessage(), e);                ie.setStackTrace(EMPTY_STACK_TRACE);                throw ie;            } finally {                // Don't retain static reference on context.                mConstructorArgs[0] = lastContext;                mConstructorArgs[1] = null;                Trace.traceEnd(Trace.TRACE_TAG_VIEW);            }            return result;        }    }

重点在46-58行中 
if (root != null) {        if (DEBUG) {            System.out.println("Creating params from root: " +                    root);        }        // Create layout params that match root, if supplied        params = root.generateLayoutParams(attrs);        if (!attachToRoot) {            // Set the layout params for temp if we are not            // attaching. (If we are, we use addView, below)            temp.setLayoutParams(params);        }    }
当且仅当 root不为空 且 attachToRoot为false时,为temp设置params参数。

在73-75行中

if (root != null && attachToRoot) {        root.addView(temp, params);    }

如果root不为空且attachToRoot为ture,root会自动加载resource布局,且带有 params参数。

79-81行中
if (root == null || !attachToRoot) {        result = temp;    }
如果root不为空,且attachToRoot为false 将temp赋给result,params丢失。

3、结论

假设外层布局只有一个LinearLayout
<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    android:id="@+id/layout"></LinearLayout>

需要加载的布局为一个Button
<?xml version="1.0" encoding="utf-8"?><Button    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/button"    android:text="button"    android:layout_width="wrap_content"    android:layout_height="wrap_content" />

3.1:root为null时。

attachToRoot起不到作用,而且需要手动addView加载。

public class MainActivity extends AppCompatActivity {    private View child;    private LinearLayout layout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        layout = (LinearLayout)findViewById(R.id.layout);        child = LayoutInflater.from(this).inflate(R.layout.layout_child,null);        layout.addView(child);    }}

而此时,根布局参数无作用(另外两种情况下,从源码中可以看到都设置了params),最简单的解决方式是在button外面再套一层布局。

3.2:root不为null,attachToRoot为true时。

返回root 不需要手动addView();

layout = (LinearLayout)findViewById(R.id.layout);LayoutInflater.from(this).inflate(R.layout.layout_child,layout,true);

3.3: root不为null,attachToRoot为false时。

返回temp,需要手动addView();

layout = (LinearLayout)findViewById(R.id.layout);child = LayoutInflater.from(this).inflate(R.layout.layout_child,layout,false);layout.addView(child);