标签:sid frame ges layout ted source tor with except
public static LayoutInflater from(Context context) {
return (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
public View inflate(XmlPullParser parser, ViewGroup root) {
return inflate(parser, root, root != null);
}
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
inflate方法分析
下面分析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 (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 创建根节点的View
final View temp = createViewFromTag(root, name, inflaterContext, attrs);
ViewGroup.LayoutParams params = null;
if (root != null) {
// Create layout params that match root, if supplied 如果提供了LayoutParams
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {//即inflate(resId , parent,false)时的逻辑
// Set the layout params for temp if we are not attaching. (If we are, we use addView, below)
temp.setLayoutParams(params); // 为xml中的root设置LayoutParams
}
}
// Inflate all children under temp against its context.
rInflateChildren(parser, temp, attrs, true);
// We are supposed to attach all the views we found (int temp) to root. Do that now.
if (root != null && attachToRoot) {//即inflate(resId , parent,true)时的逻辑
root.addView(temp, params); //将temp按照params添加到root中
}
// Decide whether to return the root that was passed in or the top view found in xml.
if (root == null || !attachToRoot) { //即inflate(resId , null)时的逻辑
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;
}
}
public void addView(View child) {
throw new UnsupportedOperationException("addView(View) is not supported in AdapterView");
}
下面我们来总结下inflate(int resourceId, ViewGroup root, boolean attachToRoot)方法中这三个参数的作用首先,resourceId是布局Id,root是这个布局所依附的父布局,attachToRoot就是是否依附在这个父布局上,下面分情况讨论:
- 如果 root 为null,此时 attachToRoot 的值将不起作用,resource的最外层的控件的宽高是【没有】效果的,并且会添加到父控件中
- 如果 root 不为null,attachToRoot 为 false:resource的最外层的控件的宽高是【有】效果的,但不会添加到父控件中
- 如果 root 不为null,attachToRoot 为 true: resource的最外层的控件的宽高是【有】效果的,并且会添加到父控件中
/**
* Set the layout parameters associated管理 with this view. These supply提供
* parameters to the <i>parent父布局</i> of this view specifying指定 how it should be
* arranged安排. There are many subclasses子类 of ViewGroup.LayoutParams, and these
* correspond to the different subclasses of ViewGroup that are responsible承担
* for arranging their children.
*
* @param params The layout parameters for this view, cannot be null
*/
public void setLayoutParams(ViewGroup.LayoutParams params) {
if (params == null) {
throw new NullPointerException("Layout parameters cannot be null");
}
mLayoutParams = params;
resolveLayoutParams();
if (mParent instanceof ViewGroup) {
((ViewGroup) mParent).onSetLayoutParams(this, params);
}
requestLayout();
}
public static class LayoutParams extends ViewGroup.MarginLayoutParams
public static class MarginLayoutParams extends ViewGroup.LayoutParams
LayoutInflater inflate LayoutParams 简介
标签:sid frame ges layout ted source tor with except
原文地址:http://www.cnblogs.com/baiqiantao/p/6d2f973e8d98f28ff220609966b26f3e.html