inflate方法有如下两种:
public View inflate (int resource, ViewGroup root)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
查看源码,我们发现两个参数的方法内部引用了三个参数的方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
我们将两个参数的方法转化为三个参数的方法。如下:
inflater.inflate(R.layout.item, null);
—————>inflater.inflate(R.layout.item, null, null != null);
相当于:
inflater.inflate(R.layout.item, null, false);
inflater.inflate(R.layout.item, parent);
—————>inflater.inflate(R.layout.item, parent, parent!= null);
相当于:
inflater.inflate(R.layout.item, parent, true);
所以我们只需要研究四个方法
inflater.inflate(R.layout.item, null, true);
inflater.inflate(R.layout.item, null, false);
inflater.inflate(R.layout.item, parent, true);
inflater.inflate(R.layout.item, parent, false);
参数详解
第一个参数:想要添加的布局
第二个参数:想要添加到哪个布局上面
(null和有值的区别 null时第一个参数中最外层的布局大小无效,有值的时候最外层的布局大小有效)
第三个参数:是否直接添加到第二个参数布局上面
(true代表layout文件填充的View会被直接添加进parent,而传入false则代表创建的View会以其他方式被添加进parent)
代码演示
1.主布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/root"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="你好啊"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:text="切换布局1"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/button1" />
<Button
android:text="切换布局2"
android:gravity="center"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:id="@+id/button2" />
</LinearLayout>
2.两个分别待插入的布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="200dp"
android:layout_height="200dp"
android:background="#00FFFF"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p1"
android:layout_margin="10dp"
android:id="@+id/image"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="200dp"
android:layout_height="200dp"
android:background="#FF00FF"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/p3"
android:layout_margin="10dp"
android:id="@+id/image"/>
</LinearLayout>
这两个布局中在外层布局中都设置了宽高,为button增加点击事件
1.inflate(R.layout.layout1,null,true);
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
root.removeAllViews();
/* if(root.findViewById(R.id.image)!=null) {
View oldview = root.findViewById(R.id.image);
root.removeView(oldview);
}*/
View view=LayoutInflater.from(this).inflate(R.layout.layout1,null,true);
//LayoutInflater.from(this).inflate(R.layout.layout1,root,true);
root.addView(view);
break;
}
switch (v.getId()){
case R.id.button2:
root.removeAllViews();
/* if(root.findViewById(R.id.image)!=null) {
View oldview = root.findViewById(R.id.image);
root.removeView(oldview);
}*/
View view=LayoutInflater.from(this).inflate(R.layout.layout2,null,true);
// LayoutInflater.from(this).inflate(R.layout.layout2,root,true);
root.addView(view);
break;
}
2.inflate(R.layout.layout1,null,false);
3.inflate(R.layout.layout1,root,true);(注这样写的话,需删除root.addView(view)这句。)
或者inflate(R.layout.layout1,root,false);
可以看到如果第二个参数不为null,那么第一个参数所引用的布局文件的外围布局设置的宽高就有效,否则就无效。