《Android学习笔记》:Infalte()方法的理解

Inflate()方法有这样两种形式:
inflate(int resource, ViewGroup root)
inflate(int resource, ViewGroup root, boolean attachToRoot)
其中,resource是我们要加载的布局文件的id,root是指定布局要加载到哪一个容器中,attachToRoot待会详细说。

定义parent.xml和child_xml布局文件:

parent.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    android:orientation="vertical">



</LinearLayout>
child.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/child"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:orientation="horizontal">

    <Button
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="你好" />

</LinearLayout>
  1. LayoutInflater.from(this).inflate(R.layout.child, parent, false)的情况
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);

        LinearLayout parent = findViewById(R.id.parent);
        LayoutInflater.from(this).inflate(R.layout.child, parent, false);
    }
}

执行上述代码,可以看到啥child.xml并没有加载到parent.xml中。
在这里插入图片描述

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);

        LinearLayout parent = findViewById(R.id.parent);
        View view = LayoutInflater.from(this).inflate(R.layout.child, parent, false);
        parent.addView(view);
    }
}

执行上述代码,可以看到child.xml添加到了parent.xml中。
在这里插入图片描述

  1. LayoutInflater.from(this).inflate(R.layout.child, parent, true)的情况
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);

        LinearLayout parent = findViewById(R.id.parent);
        LayoutInflater.from(this).inflate(R.layout.child, parent, true);
        
    }
}

可以看到child.xml成功添加到parent.xml中。
在这里插入图片描述
综合1和2,我们可以发现:attachToRoot=false的情况下,如果没有parent.addView()这句,child.xml是不会添加到parent.xml中的。attachToRoot=true的情况下,就算没有parent.addView()这句,child.xml也会添加到parent.xml中。
所以我们可以得到这样一个结论:attachToRoot的作用就是——是否将第一个参数指定的布局文件添加到第二个参数指定的容器中。

在学习inflate()方法的时候,了解到第二个参数root也有很多的学问,原理我也不是很懂,不过我觉得很有道理。先看下它的值的情况。

  1. LayoutInflater.from(this).inflate(R.layout.child, null, false)的情况
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);

        LinearLayout parent = findViewById(R.id.parent);
        LayoutInflater.from(this).inflate(R.layout.child, null, false);

    }
}

在这里插入图片描述
运行可以看到,child.xml并没有添加到parent.xml中,不难理解,root=null,表示的意思就是不会将第一个参数指定的布局添加到任何容器中。attachToRoot=false不是也有这样的作用吗?其实,就算为true,没有指定容器,一样不会添加child.xml。
那我们可不可以手动添加呢?也就是通过parent.addView()方法来添加child.xml呢?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.parent);

        LinearLayout parent = findViewById(R.id.parent);
        View view = LayoutInflater.from(this).inflate(R.layout.child, null, false);
        parent.addView(view);
    }
}

在这里插入图片描述
看图发现的确有添加进来,但是注意我们之前对child的LinearLayout的宽高的设置(android:layout_width=“100dp”
android:layout_height=“100dp”)
在这里怎么会显示一个长条呢?
按照https://blog.csdn.net/u012702547/article/details/52628453这位博客的说法,就是:如果root=null,不指定容器给第一个参数,那么第一个参数指定的布局它所对应的宽高属性就会失效!

好了,我也就懂这么多,源码什么的本人还没有那个水平看懂,只能按照上面的记一下了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值