总结:LayoutInflater中inflate里面的true

2018-04-10  本文已影响17人  一个冬季

转载地址https://blog.csdn.net/u012702547/article/details/52628453 (讲的非常好)

我们一般加入一个View进来的方式是哪种?

 ...
       LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.linearlayout, ll,false);
        ll.addView(view);//把视图加载进来
...

是不是像上面那样把一个View加载进来

常见疑惑1:我把false改为true会怎样?

如果你把false改为true,ll.addView(view);这句代码就要删除了否则会报如下错。

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. 

删除后正确的代码如下(最后显示的效果同上):

        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        LayoutInflater inflater = LayoutInflater.from(this);
        View view = inflater.inflate(R.layout.linearlayout, ll,true);
常见疑惑2:我将root设置为null会怎样?

不将resource添加root中,我第三个参数是true还是false都没有任何意义了。里面设置的宽高都不会有任何的价值。

上一篇 下一篇

猜你喜欢

热点阅读