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, 
 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值