总结:
当父容器宽高失效时,加入该布局,则取决于子控件的布局参数
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);
}