[Android]LayoutInflater的inflate方法半详解

一、Activity中的setContentView

@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);

setContentView()的android源码如下:

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

其中getWindow()返回值类型为PhoneWindow,

首先我们要明白Window与Activity之间的关系。在源码中可以看到Activity的两个成员变量,分别是:

private Window mWindow;private WindowManager mWindowManager;

而mWindow在Activity的attach()方法中被赋值,代码如下:

mWindow = PolicyManager.makeNewWindow(this);

跟进去可以看到它返回的是一个PhoneWindow对象,它是Window的子类。现在再返回去setContentView()的源码,其中关键的就是getWindow().setContentView(layoutResID);这一句;其中getWindow()方法其实就是返回Activity的成员变量mWindow,因此Activity的setContentView()实质上是调用PhoneWindow的setContentView()方法。接着看源码:

@Overridepublic void setContentView(int layoutResID) {if (mContentParent == null) {installDecor();} else {mContentParent.removeAllViews();}mLayoutInflater.inflate(layoutResID, mContentParent);final Callback cb = getCallback();if (cb != null && !isDestroyed()) {cb.onContentChanged();}}从上面的代码看到,首先判断nstallDecor()方法用于初始化mContentParent。

二、LayoutInflater.inflate()方法

Instantiates a layout XML file into its correspondingView objects. It is never used directly. Instead, usegetLayoutInflater() orgetSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on. For example:

LayoutInflater inflater=

LayoutInflater实例,这个LayoutInflater实例已经绑定到了当前的context并且在当前正在运行的机器上进行过了正确的配置。举例如下:

LayoutInflater inflater=

)实质上就是通过上面举得例子的方法获取到的。

接下来看它的inflate()方法,源码中可以看见,inflate()方法有四种重载方式,分别如下:

public View inflate(int resource, ViewGroup root)

public View inflate(XmlPullParser parser, ViewGroup root)

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

public View inflate(XmlPullParser parser, ViewGroup root,boolean attachToRoot)

它们的返回值虽然类型都为View,但在不同的情况下它们的返回值可不见得都是通过传入XML布局资源实例化出来的View,不信?我们慢慢来看。

我们首先来看前三种重载方法的源码。

public View inflate(int resource, ViewGroup root) {return inflate(resource, root, root != null);}public View inflate(XmlPullParser parser, ViewGroup root) {return inflate(parser, root, root != null);}public View inflate(int resource, ViewGroup root, boolean attachToRoot) {final Resources res = getContext().getResources();if (DEBUG) {Log.d(TAG, "INFLATING from resource: \&;" + res.getResourceName(resource) + "\&; ("+ Integer.toHexString(resource) + ")");}final XmlResourceParser parser = res.getLayout(resource);try {return inflate(parser, root, attachToRoot);} finally {parser.close();}}

可以看出,这三种方法实际上都是直接或间接调用了第四种重载方法。我们首先来看这个方法的API文档:

Inflate a new view hierarchy from the specified XML node. ThrowsInflateException if there is an error.

ImportantFor performance reasons, view inflation relies heavily on pre-processing of XML files that is done at build time. Therefore, it is not currently possible to use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

Parameters

parser

XML dom node containing the description of the view hierarchy.

root

Optional view to be the parent of the generated hierarchy (ifattachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (ifattachToRoot is false.)

attachToRoot

Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.

后来逐渐有广州花城的,

[Android]LayoutInflater的inflate方法半详解

相关文章:

你感兴趣的文章:

标签云: