Android:View.inflate和getLayoutInflater()之间的区别是什么?inflate?

Den*_*nis 5 layout android layout-inflater

真正的区别是什么:

return context.getLayoutInflater().inflate(R.layout.my_layout, null);
Run Code Online (Sandbox Code Playgroud)

从指定的xml资源中扩充新的视图层次结构.

return View.inflate(context, R.layout.my_layout, null);
Run Code Online (Sandbox Code Playgroud)

从XML资源中扩展视图.这个便捷方法包装了LayoutInflater类,它为视图通胀提供了全方位的选项.

Taf*_*hdi 6

两者都是一样的.第二个版本只是一个方便而简短的方法来完成任务.如果您看到View.inflate()方法的源代码,您会发现:

 /**
     * Inflate a view from an XML resource.  This convenience method wraps the {@link
     * LayoutInflater} class, which provides a full range of options for view inflation.
     *
     * @param context The Context object for your activity or application.
     * @param resource The resource ID to inflate
     * @param root A view group that will be the parent.  Used to properly inflate the
     * layout_* parameters.
     * @see LayoutInflater
     */
    public static View inflate(Context context, int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }
Run Code Online (Sandbox Code Playgroud)

在后端实际上做同样的工作,你提到的第一种方法.