一、API文档解读
翻译:将布局XML文件实例化为其对应的View对象。它不能通过new关键词来实例化。应该通过Activity.getLayoutInflater()或Context.getSystemService(Class)来实例化。
.......
必须使用带有参数LayoutInflater.class的Context.getSystemService(Class)或带有Context.LAYOUT_INFLATER_SERVICE参数的Context.getSystemService(String)来获取此类的实例。
实例化①:在Activity环境下
LayoutInflater layoutInflater = this.getLayoutInflater();
实例化②:在Context环境下
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
实例化③:在Context环境下。这个方法是Android6.0版本提供的
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
LayoutInflater systemService = this.getSystemService(LayoutInflater.class);
}
实例化④:从给定的上下文中获取实例。
LayoutInflater layoutInflater = LayoutInflater.from(this);
二、inflate()方法解读
LayoutInflater的方法中我们平常用的最多的就是inflate()方法,该方法有四种重载方法。
// 方法1
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
// 方法2
public View inflate(XmlPullParser parser, @Nullable ViewGroup root)
// 方法3
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
// 方法4
public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
方法1,3是我们常用的方法,这边文章的主要目的就是研究不同的参数对我们使用的影响。
先看方法3:
情形①:root != null && attachToRoot = true
表示将resource指定的布局添加到root中,添加的过程中resource所指定的布局的根节点的各个属性都是生效的。
案例:
Activity的布局:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/ll_container"
tools:context=".MainActivity">
</LinearLayout>
另外一个带添加的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="@color/colorPrimary"
android:gravity="center"
android:layout_margin="16dp"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
想将布局文件添加到到activity_main布局中,可以这么做:
LinearLayout container = findViewById(R.id.ll_container);
LayoutInflater factory = LayoutInflater.from(this);
factory.inflate(R.layout.layout_test, container, true);
效果图如下
情形②:root != null && attachToRoot = false
表示resource指定的布局的根节点的layout_*属性是生效的,但是不将这个布局添加到我们的root布局中。
如果要将布局添加到我们的布局中,该如何操作了?
LinearLayout container = findViewById(R.id.ll_container);
LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.layout_test, container, false);
container.addView(view);
为什么attachToRoot是true和false的时候,一个默认直接添加到我们的布局,另外一个要手动的调用addView()方法,查看源码可知:
当attach=true的时候,调用了root.addView()方法;而当attach=false的时候,只是将布局参数设置了进来,但是并没有调用addView()方法。
情形③:root == null && attachToRoot = ture
情形④:root == null && attachToRoot = false
代码如下:
LinearLayout container = findViewById(R.id.ll_container);
LayoutInflater factory = LayoutInflater.from(this);
View view = factory.inflate(R.layout.layout_test, null, false);
container.addView(view);
效果图如下:
当root=null的时候,不论attachToRoot是true或者false,显示效果都是一样。layout_*都是失效的。
再看方法1:
情形⑤:root不为 null
根据源码可知和情形①:root != null && attachToRoot = true的情况时一样的。
源码如下:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
情形⑥:root 为null
根据源码可知和情形④:root == null && attachToRoot = false是一样的。
源码如下:
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
return inflate(resource, root, root != null);
}
文章暂时结束。欢迎各位客官品评。
参考文章:
[_江南一点雨]
https://blog.csdn.net/u012702547/article/details/52628453
[低调小一]
https://blog.csdn.net/wzy_1988/article/details/52095398