addView遇到的坑及其解决

   代码中给容器动态添加子View时遇到一些问题,当时还是纠结许久的。擅总结者无敌,写下此篇总结,问题比较的简单,但也包含了成长与反思。

 

使用场景:

情况一:

View view = View.inflate(this, R.layout.item_contact,null);

view.getLayoutParams() == null

情况二:

View view =getLayoutInflater().inflate(R.layout.item_contact, null, false);

view.getLayoutParams() == null

情况三:

View view =getLayoutInflater().inflate(R.layout.item_contact, mContainer, false);

或者:View view =LayoutInflater.from(this).inflate(R.layout.item_contact, mContainer, false);

view.getLayoutParams() != null

 

//添加到指定父容器

mContainer.addView(view);

 

View.inflate(this, R.layout.item_contact, mContainer);

作用:创建一个指定布局的View对象,并加入到mContainer的父容器中。

 

getLayoutInflater().inflate(R.layout.item_contact,mContainer, true);

作用:创建一个指定布局的View对象,并加入到mContainer的父容器中。

 

View.inflate(this,resource, null);

这种方式创建的View对象没有LayoutParams。没指定父容器(null),也就是宽高参数是空的。

此时addView方法中会调用generateDefaultLayoutParams()生成默认的宽高参数:

就是:new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

如果你的原本布局宽高不是wrap_content,wrap_content,那么就有问题了。

 

LayoutInflater.inflate方法

LayoutInflater.inflate(int resource, ViewGroup root,boolean attachToRoot)

resource:资源文件id

root:父容器对象

attachToRoot:是否添加到父容器中

备注:

attachToRoot == true;root会作为创建的View对象的父容器

attachToRoot == false;root会为创建的View对象提供LayoutParams参数

 

对于新手而言,问题可能在于:

View view = View.inflate(this, R.layout.item_contact,null);

mContainer.addView(view);添加到mContainer父容器中

 

问题在于:此时的view是没有LayoutParams的,需要:

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(

LinearLayout.LayoutParams.WRAP_CONTENT,

LinearLayout.LayoutParams.MATCH_PARENT);

需要使用:mContainer.addView(view, params);这样才能正确体现View布局的宽高参数。

上述代码相当于如下:

View.inflate(this, R.layout.item_contact, mContainer);//一行代码搞定

而同一个View对象,不能同时有两个父容器,此时就无需再调用addView(view);添加了。

如果不需要对View对象进行操作,还免去创建引用变量的资源消耗。(这里有坑)

 

另外一个类似方法:

getLayoutInflater().inflate(int resource, ViewGrouproot, boolean attachToRoot)

事实上,从源码可见:

View.inflate(this, resource, null);//实现原理可归纳为:

LayoutInflater.from(this).inflate(resource, root, root!= null);

 

总结:为容器添加子View的方式:

方式一://方便重新指定LayoutParams

View view = View.inflate(this, R.layout.item_contact,null);

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(...);//重新指定LayoutParams。

mContainer.addView(view, params);

方式二(同一):

View view =LayoutInflater.from(this).inflate(R.layout.item_contact, mContainer, false);

mContainer.addView(view);

 

方式三://适合于使用布局中指定的LayoutParams

View.inflate(this, R.layout.item_contact, mContainer);

方式四(同四):

LayoutInflater.from(this).inflate(R.layout.item_contact,mContainer, true);

 

备注:显然,方式三、四使用更简洁,方式一、二则适用于布局重用于不适合布局中指定的LayoutParams的情况。

==================================================================================================

坑坑坑坑

LayoutInflater的API文档:

View inflate (int resource, ViewGroup root)

The root View of the inflated hierarchy. If root was supplied, this is the root View; otherwise it is the root of the inflated XML file.


View inflate (int resource, ViewGroup root, boolean attachToRoot)

The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.


例如:

View view = View.inflate(this, resoutceId, mContainer);

如果传入root不为空,

那么此时view表示mContainer和新创建出来的对象结合后的root View,这里就是父容器mContainer。

当你需要对每个新创建的View对象进行操作时,如统一点击事件等,则不能传入父容器。


View view = View.inflate(this,resoutceId, null);

mContainer.addView(view);

如果传入root为空

那么此时view表示新创建出来的View对象,就是resoutceId指定的布局的根元素。

例如:resoutceId根元素是TextView,则view是TextView对象


另外:

LayoutInflater.from(this).inflate(resoutceId, mContainer));

//默认attach到mContainer,返回mContainer


LayoutInflater.from(this).inflate(resoutceId, mContainer, true));

//attach到mContainer,返回mContainer


LayoutInflater.from(this).inflate(resoutceId, mContainer, false)); 

//没有attach到mContainer,返回resoutceId的根元素



  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值