addView導致子佈局“match_parent”屬性失效詳解

問題

項目中我們經常會使用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, -1);
    }

    ……

 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));
發佈了87 篇原創文章 · 獲贊 364 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章