安卓inflate

总结:

当父容器宽高失效时,加入该布局,则取决于子控件的布局参数

 

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

一、root不为null

①root不为null,attachToRoot为true

layoutRes自动添加到root布局中

②root不为null,attachToRoot为false

不会自动添加,需要加上root.addView(view)

(代码)

===false

LayoutInflater inflater = LayoutInflater.from(this);

View view = inflater.inflate(R.layout.LinearLayout1, LinearLayout2, false);

LinearLayout2.addView(view);

(补充:为false时,param2第二个参数只是提供一个容器,为了“保留”住LinearLayout1子布局的宽高,防止其失效,所以param2 可以换成任意布局,不过也没什么意思)

===true

LayoutInflater inflater = LayoutInflater.from(this);

View view = inflater.inflate(R.layout.LinearLayout1, LinearLayout2, true);

二、root为null

root为null,attachToRoot为true时也会失效 (你都不知道root是谁,自动加,加给谁我咋知道??)

同时layoutRes的宽高失效,并仍需将(XX布局).addView(view)

 

LayoutInflater inflater = LayoutInflater.from(this);

View view = inflater.inflate(R.layout.LinearLayout1, null, 失效);

LinearLayout2.addView(view);

 

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root)

一、root不为null

设为true(自动加addView)

二、root为null

设为false(都没有root,加不加无所谓,设为true也没用)

(源码呈上,多说无益)

public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
    return inflate(resource, root, root != null);
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值