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>
- 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中。
- 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也有很多的学问,原理我也不是很懂,不过我觉得很有道理。先看下它的值的情况。
- 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,不指定容器给第一个参数,那么第一个参数指定的布局它所对应的宽高属性就会失效!
好了,我也就懂这么多,源码什么的本人还没有那个水平看懂,只能按照上面的记一下了。