問題
項目中我們經常會使用
inflater
動态生成并加載
View
,例如:
View v = inflater.inflate(R.layout.layout_child, null);
parent.addView(v);
而在最後
addView
的時候,也會經常遇到詭異的現象:
明明子布局我們設定的是
android:layout_width="match_parent"
,而最後真機的效果卻是
"wrap_content"
,簡直莫名其妙!
為什麼會這樣?
遇到這種詭異的bug,不妨先讀讀相關源碼。
源碼追蹤
我們看下addView的源碼。
public void addView(View child) {
addView(child, -);
}
……
public void addView(View child, int index) {
if (child == null) {
throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup");
}
LayoutParams params = child.getLayoutParams();
if (params == null) {
params = generateDefaultLayoutParams();
if (params == null) {
throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null");
}
}
addView(child, index, params);
}
可以看到,我們直接調用
addView
方法的時候,首先會擷取子布局的參數:
LayoutParams params = child.getLayoutParams();
而這個方法的注解中有這樣一段說明:
This method may return null if this View is not attached to a parent
ViewGroup or {@link #setLayoutParams(android.view.ViewGroup.LayoutParams)}
was not invoked successfully. When a View is attached to a parent
ViewGroup, this method must not return null.
當View沒有挂載到父布局或者沒有成功生成時,它的參數為null!
而剛好,我們前一步隻是動态生成了
View
,還沒有挂到父布局,是以接下來,會調用預設參數
generateDefaultLayoutParams()
:
protected LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
}
看到了吧,預設參數是寬高都為
WRAP_CONTENT
。
總結
當我們使用
inflater
生成子
View
,然後直接調用
addView(View v)
方法時,子布局的寬高設定都會失效!
解決方案
我們隻需要在addView的時候,賦予子布局一個布局參數即可:
parent.addView(v, new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));