Android--LayoutInflater的渲染过程

本文深入解析LayoutInflater的inflate方法,从资源加载、View创建到添加到父布局的过程,讲解了resource、root和attachToRoot参数的作用,揭示了 inflate 的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LayoutInflater我们应该都很熟悉了,我们在它的inflate中传入对应的布局id,就可以得到一个View,LayoutInflater是如何做到的呢?我们今天来探索看看。

目录

一、LayoutInflater的inflate方法

二、inflate方法中各个参数的含义


一、LayoutInflater的inflate方法

我们先贴出来一个布局文件以及LayoutInflater的调用过程

布局文件R.layout.test_layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <LinearLayout
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

LayoutInflater的调用如下(当然,是有重载方法的,我们以下面这个为例)

LayoutInflater.from(this).inflate(R.layout.test_layout,parent)

我们先进入inflate中查看

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();

        View view = tryInflatePrecompiled(resource, res, root, attachToRoot);
        if (view != null) {
            return view;
        }
        XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

调用了重载方法inflate

    public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            ....

            try {
                advanceToRootNode(parser);
                final String name = parser.getName();

                if (TAG_MERGE.equals(name)) {
                    .....
                } 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) {
                        ....
                        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);
                        }
                    }

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

            } .....

            return result;
        }
    }

我精简了一下代码

第9行:这里主要就是判断xml的节点是否为merge,我们的例子中没有merge节点,所以略过

第13行:这个方法就是根据节点,来创建View对象,这里是重点,我们进去看看

    private View createViewFromTag(View parent, String name, Context context, AttributeSet attrs) {
        return createViewFromTag(parent, name, context, attrs, false);
    }

重载方法

    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
        ....

        try {
            View view = tryCreateView(parent, name, context, attrs);

            if (view == null) {
                final Object lastContext = mConstructorArgs[0];
                mConstructorArgs[0] = context;
                try {
                    if (-1 == name.indexOf('.')) {
                        view = onCreateView(context, parent, name, attrs);
                    } else {
                        view = createView(context, name, null, attrs);
                    }
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
            }

            return view;
        } 
        ....
    }

第6行:tryCreateView方法返回的View不是空,这里我们先不解释为什么,我们假设它是空。是不是空,其中的代码相似,所以我们先假设是空。

第12~16行:这里的if-else主要就是判断节点是否包含“.”,什么样的节点有点呢?一些三方库的控件,而原生的控件比如TextView,Button这些都是没有“.”的。(AndroidX的控件不是原生的)

我们的例子中,这里的name是androidx.constraintlayout.widget.ConstraintLayout,是包含“.”的,所以执行createView方法,我们看一下源码:

    public final View createView(@NonNull Context viewContext, @NonNull String name,
            @Nullable String prefix, @Nullable AttributeSet attrs)
            throws ClassNotFoundException, InflateException {
        .....

        try {

            if (constructor == null) {
                // Class not found in the cache, see if it's real, and try to add it
                clazz = Class.forName(prefix != null ? (prefix + name) : name, false,
                        mContext.getClassLoader()).asSubclass(View.class);

                if (mFilter != null && clazz != null) {
                    boolean allowed = mFilter.onLoadClass(clazz);
                    if (!allowed) {
                        failNotAllowed(name, prefix, viewContext, attrs);
                    }
                }
                constructor = clazz.getConstructor(mConstructorSignature);
                constructor.setAccessible(true);
                sConstructorMap.put(name, constructor);
            } else {
                .....
            }

            Object lastContext = mConstructorArgs[0];
            mConstructorArgs[0] = viewContext;
            Object[] args = mConstructorArgs;
            args[1] = attrs;

            try {
                final View view = constructor.newInstance(args);
                if (view instanceof ViewStub) {
                    // Use the same context when inflating ViewStub later.
                    final ViewStub viewStub = (ViewStub) view;
                    viewStub.setLayoutInflater(cloneInContext((Context) args[0]));
                }
                return view;
            } finally {
                mConstructorArgs[0] = lastContext;
            }
        }.....

    }

方法的内容虽然不少,但是总的来说很好理解:name此时是全路径的类名,我们根据全路径类名,通过反射拿到View对象,然后返回。这个生成的View对象最终会返回到inflate方法中createViewFromTag调用的地方,我再贴一遍上面的inflate方法

    public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            ....

            try {
                advanceToRootNode(parser);
                final String name = parser.getName();

                if (TAG_MERGE.equals(name)) {
                    .....
                } 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) {
                        ....
                        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);
                        }
                    }

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

            } .....

            return result;
        }
    }

上面的View对象,在inflate方法中就是temp对象

第17~25行:这里和本次的流程关系不大,但是可以简单说一下。LayoutInflater的inflate方法中,第三个参数就是attachToRoot。而我们的例子中调用的是两个参数的重载方法,默认attachToRoot是true。如果我们使用三参数的inflate方法,并且attachToRoot为false,就会执行到这里了。当attachToRoot为false,那么我们刚刚创建对象的LayoutParams使用的就是我们所传入的root的LayoutParams

第28行:这里会从temp中获取子View进行渲染,我们进入

    final void rInflateChildren(XmlPullParser parser, View parent, AttributeSet attrs,
            boolean finishInflate) throws XmlPullParserException, IOException {
        rInflate(parser, parent, parent.getContext(), attrs, finishInflate);
    }

进入rInflate方法

    void rInflate(XmlPullParser parser, View parent, Context context,
            AttributeSet attrs, boolean finishInflate) throws XmlPullParserException, IOException {

        final int depth = parser.getDepth();
        int type;
        boolean pendingRequestFocus = false;

        while (((type = parser.next()) != XmlPullParser.END_TAG ||
                parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {

            if (type != XmlPullParser.START_TAG) {
                continue;
            }

            final String name = parser.getName();

            if (TAG_REQUEST_FOCUS.equals(name)) {
                pendingRequestFocus = true;
                consumeChildElements(parser);
            } else if (TAG_TAG.equals(name)) {
                parseViewTag(parser, parent, attrs);
            } else if (TAG_INCLUDE.equals(name)) {
                if (parser.getDepth() == 0) {
                    throw new InflateException("<include /> cannot be the root element");
                }
                parseInclude(parser, context, parent, attrs);
            } else if (TAG_MERGE.equals(name)) {
                throw new InflateException("<merge /> must be the root element");
            } else {
                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);
            }
        }
        ....
    }

第8行:这里while循环的意义就是循环获取我们例子中ConstraintLayout的子View,此时的子View是在xml中表现出来的,还没真正渲染出来

第17~29行:这些if和else if就是当标签为一些特殊情况的处理,比如include,merge等,而我们的例子中,真正执行的是else

第30~34行:这里有熟悉的代码,比如createViewFromTag,rInflateChildren。这些都是刚刚说过的流程,不过有个地方需要要注意一下,我们重新看一下createViewFromTag方法

    View createViewFromTag(View parent, String name, Context context, AttributeSet attrs,
            boolean ignoreThemeAttr) {
        ....

        try {
            View view = tryCreateView(parent, name, context, attrs);

            if (view == null) {
                final Object lastContext = mConstructorArgs[0];
                mConstructorArgs[0] = context;
                try {
                    if (-1 == name.indexOf('.')) {
                        view = onCreateView(context, parent, name, attrs);
                    } else {
                        view = createView(context, name, null, attrs);
                    }
                } finally {
                    mConstructorArgs[0] = lastContext;
                }
            }

            return view;
        } 
        ....
    }

第12~16行:上面我们执行到这里的时候,标签为androidx.constraintlayout.widget.ConstraintLayout,包含“.”;而此时标签为TextView,并没有“.”,所以执行的应该是createView。但是要注意的是这里的createView并不是LayoutInflater,LayoutInflater实际上是一个抽象类,而我们通过LayoutInflater.from获取的对象其实是PhoneLayoutInflater,PhoneLayoutInflater重写了createView,我们看一下这个方法

    @Override protected View onCreateView(String name, AttributeSet attrs) throws ClassNotFoundException {
        for (String prefix : sClassPrefixList) {
            try {
                View view = createView(name, prefix, attrs);
                if (view != null) {
                    return view;
                }
            } catch (ClassNotFoundException e) {
                // In this case we want to let the base class take a crack
                // at it.
            }
        }

        return super.onCreateView(name, attrs);
    }

sClassPrefixList是一个前缀的列表

    private static final String[] sClassPrefixList = {
        "android.widget.",
        "android.webkit.",
        "android.app."
    };

for循环这个列表,拿到前缀和我们例子中的TextView,传到LayoutInflater中createView方法里。上面我们说过,createView方法就是根据全类名反射获取View对象。我们的例子是TextView,所以全类名为android.widget.TextView(全类名不对的话,反射获得的View对象肯定为空);如果这些全类型都不对怎么办?比如SurfaceView,它的全类型是android.view.SurfaceView。这时候会执行super.onCreateView,也就是LayoutInflater的onCreateView方法:

    protected View onCreateView(String name, AttributeSet attrs)
            throws ClassNotFoundException {
        return createView(name, "android.view.", attrs);
    }

LayoutInflater中添加了android.view的前缀

到这里,LayoutInflater的渲染过程结束了。不了解LayoutInflater的inflate方法的时候,觉得很神秘。看了源码才发现,真的挺简单的,就是解析标签,拿到View或者ViewGroup;解析下一层级的xml标签,然后通过addView添加到父布局中,就是这么简单而已。

二、inflate方法中各个参数的含义

inflate方法有两参数的重载方法,但是最终调用的还是三参数方法

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

这三个参数的含义如下:

resource:这个就是我们自己layout的id,没什么好说的

root和attachToRoot:

我们先看inflate方法中的代码:

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

result是inflate方法返回的对象

如果root不等空并且attachToRoot为true,则将渲染后得到的View添加到root中,返回的result就是root;

如果root为空或者attachToRoot为false,则将渲染后得到的View赋值给result返回

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值