在Android開發(fā)中咆槽,我們通過LayoutInflater去解析布局文件生成對應(yīng)的View對象收擦。
方法參數(shù):
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
由于傳入的方法參數(shù)不同哆键,該方法返回的view也會不同苞轿,接下來通過查看源碼嗜傅,來探究其處理邏輯金句。
為了讓代碼篇幅短一些,我省掉了一些不影響邏輯的代碼吕嘀,在重要的地方加了注釋违寞,其他代碼邏輯讀者可以查看源碼。
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
...
//通過傳入的布局資源文件獲取xml資源解析對象
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
inflate內(nèi)部調(diào)用了inflate(parser, root, attachToRoot)偶房;該方法實現(xiàn)了解析布局文件趁曼。
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
synchronized (mConstructorArgs) {
final AttributeSet attrs = Xml.asAttributeSet(parser);
...
View result = root;
// Temp is the root view that was found in the xml
//這里獲取的是資源文件根布局的對象
final View temp = createViewFromTag(root, name, attrs, false);
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
//獲取根布局的Layoutparams屬性
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
//給布局資源根對象設(shè)置布局文件最外層layout屬性
temp.setLayoutParams(params);
}
}
...
// Inflate all children under temp
rInflate(parser, temp, attrs, true, true);
..
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
//將布局文件加入root中
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;
}
}
return result;
}
通過布局文件的屬性集attrs,來獲取資源布局文件最外層的LayoutParams的屬性
params = root.generateLayoutParams(attrs);
他的內(nèi)部實現(xiàn)為:
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LayoutParams(getContext(), attrs);
}
public LayoutParams(Context c, AttributeSet attrs) {
TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout);
setBaseAttributes(a,
R.styleable.ViewGroup_Layout_layout_width,
R.styleable.ViewGroup_Layout_layout_height);
a.recycle();
}
這里需要說明一點棕洋,root的變量定義為ViewGroup挡闰,viewGroup的generateLayoutParams內(nèi)部執(zhí)行是生成LayoutParams,它內(nèi)部會去獲取layout_width與layout_height掰盘。
讓我們試著看下LinearLayout的generateLayoutParams()實現(xiàn)方式摄悯。
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LinearLayout.LayoutParams(getContext(), attrs);
}
public LayoutParams(Context c, AttributeSet attrs) {
super(c, attrs);
TypedArray a = c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout);
weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0);
gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1);
a.recycle();
}
可以發(fā)現(xiàn)在LinearLayout中不僅獲取了layot_width和layout_height,還獲取了weight和gravity愧捕。
通過源碼的實現(xiàn)邏輯奢驯,可以總結(jié)如下:
- root不為空時,attchToRoot為true時次绘,執(zhí)行
final View temp = createViewFromTag(root, name, attrs, false);
params = root.generateLayoutParams(attrs);
root.addView(temp, params);
這時解析的布局文件view對象會成為root的子View瘪阁;
2.root不為空,attchToRoot為false邮偎,執(zhí)行
final View temp = createViewFromTag(root, name, attrs, false);
params = root.generateLayoutParams(attrs);
temp.setLayoutParams(params);
result = temp;
return result;
這里面將布局文件最外層的layout布局參數(shù)管跺,設(shè)置給了布局文件View的LayoutParams字段。
3.root為空時,執(zhí)行
final View temp = createViewFromTag(root, name, attrs, false);
result = temp;
return result;
這種解析出來的View對象是LayoutParams屬性字段為空钢猛,在調(diào)用addView()方法時伙菜,Android系統(tǒng)會默認(rèn)生成一個寬高均為wrap_content屬性的LayoutParams。
總結(jié):之所以這樣寫命迈,是建立在一個很重要的原因上贩绕,那就是layout_width這個表示的是View 在布局中的寬度 火的。對與LayoutInfalter來說,去解析一個布局文件淑倾,如果你不將該view加入ViewGroup中去的馏鹤,就沒必要獲取這個view“在布局中的屬性了”,
在使用View temp = createViewFromTag(root, name, attrs, false);去獲取View時娇哆,得到的View對象是沒有 布局屬性的湃累,所以才有了LayoutInfalter該方法。
view的屬性字段中有 protected ViewGroup.LayoutParams mLayoutParams;
temp.setLayoutParams(params);該方法給布局根View設(shè)置了“在布局中的屬性”
另外generateLayoutParams(attrs);不同ViewGroup的該方法實現(xiàn)過程不同碍讨,但是她們都沒有獲取根布局的 layout_margin屬性治力,這也就解釋了我們ListView的item布局文件中最外層節(jié)點設(shè)置了layout_margin屬性失效的原因了。