addView导致子布局“match_parent”属性失效详解

时间:2019-02-19
本文章向大家介绍addView导致子布局“match_parent”属性失效详解,主要包括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();
1
而这个方法的注解中有这样一段说明:

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,