安卓中的inflate方法

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,那么第一个参数所引用的布局文件的外围布局设置的宽高就有效,否则就无效。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值