天天看点

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, -);
    }

    ……

 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));