白驹过隙

Android之视图架构

  • LayoutInflater:

https://blog.csdn.net/guolin_blog/article/details/121889703

LayoutInflater 将一个xml布局转换成一个View对象的过程主要包含两步:

    1. 通过解析器来将xml文件中的内容解析出来。
    2. 使用反射将解析出来的元素创建成View对象。
public View inflate(int resource, @Nullable ViewGroup root, boolean attachToRoot) {
    ...
}

如果我们将inflate()方法的第三个参数传入true,那么就会直接将inflate出来的布局添加到父布局当中。这样后面再次addView的时候就会发现它已经有一个父布局了,从而抛出RuntimeException的崩溃信息。

Android之视图架构

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top