我們經常用到的布局解析器LayoutInflater,主要在ListView或者RecycleView的Adapter中,用兩個常用方法
第一個方法是 LayoutInflater.from(context).inflate(resource, root);
第二個方法是LayoutInflater.from(context).inflate(resource, root, attachToRoot);
第一個參數resource是布局,第二個root是父布局,第三個參數是決定是否把執行root.add(resource);
這兩個方法用一下四種用法:
LayoutInflater.from(context).inflate(R.layout.main_activity, null);
LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content);
LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, false);
LayoutInflater.from(context).inflate(R.layout.main_activity, android.R.id.content, true);
先上LayoutInflater的inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)方法源碼
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) {
InflateException ex = new InflateException(e.getMessage());
ex.initCause(e);
throw ex;
} catch (Exception e) {
InflateException ex = new InflateException(
parser.getPositionDescription()
+ ": " + e.getMessage());
ex.initCause(e);
throw ex;
} finally {
// Don't retain static reference on context.
mConstructorArgs[0] = lastContext;
mConstructorArgs[1] = null;
}
Trace.traceEnd(Trace.TRACE_TAG_VIEW);
return result;
}
}
由此可見
使用第一種方式是會在infalte中調用View temp = createViewFromTag(root, name, inflaterContext, attrs),但不會temp.setLayoutParams(params),是以temp;
的layoutParams為空,這是解析出來的布局的大小會有一些問題,當你在需要解析的布局中的根布局設定android:layout_width,android:layout_height不會生效;
第二種方式和第四種方式相同,會執行root.addView(temp, params),并傳回root;
第三種方式,也會直接傳回temp,但是會執行temp.setLayoutParams(params),這時在根布局設定android:layout_width,android:layout_height會生效。