zoukankan      html  css  js  c++  java
  • Android LayoutInflater.inflate()使用详解

    有一次面试,问到inflate()三个参数,平时开发经常用,但是具体细节很少追究,瞬间懵B了,找到一个比较好的文章,摘录下来。

    摘自:https://www.jianshu.com/p/c92243287793

    相信大家都用过LayoutInflater(布局填充器),今天主要说下我对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)

    下面我们来对四个方法一一讲解

    为了更好的理解我准备了一个简单的案例,我的Activity的布局如下:一个超简单的RelativeLayout 布局,我们在这理解为父布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rl"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    </RelativeLayout>
    

    我还有一个布局layout.xml如下:有一个宽度高度为200dp的LinearLayout ,LinearLayout 中有一个Button按钮。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="#ff0000">
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开开心心每一天" />
    
    </LinearLayout>
    

    布局准好了,下面进入正题。我要把layout.xml这个布局文件添加到我的activity的布局中,我的Activity的代码如下:

     
    Activity.png

    为了看到四个方法有什么不一样,我依次替换圈起来的代码,查看展示效果:

    1:inflater.inflate(R.layout.item, null, true);
     
    img.png
    2:inflater.inflate(R.layout.item, null, false);
     
    img.png
    3:inflater.inflate(R.layout.item, parent, true);

    使用该方法时rl.addView(view);代码报错,错误异常如下

    java.lang.IllegalStateException: 
    The specified child already has a parent. 
    You must call removeView() on the child's parent first.
    

    意思是非法状态异常,指定的child已经有一个父容器了。你必须先用chile的父容器调用removeView()。

    源码中对第三个参数的解释是
    Whether the inflated hierarchy should be attached to the root 
    parameter? If false, root is only used to create the correct
     subclass of LayoutParams for the root view in the XML.
    

    attachToRoot传入true代表layout文件填充的View会被直接添加进parent,而传入false则代表创建的View会以其他方式被添加进parent。

    解决方法

    删除rl.addView(view);这一行代码,然后运行展示效果

     
    img.png
    4:inflater.inflate(R.layout.item, parent, false);
     
    img.png
    总结

        从案例中我们可以看到1、2这两种方式运行结果一样,第二个参数传null会使要添加的布局视图中根视图的宽高设置失效,在使用这种方式的时候会造成我们无形中多加一层布局,为了使其子view显示相应高度。在这里不推荐使用
        3这种方式我们在使用的时候一定要注意,它会使代表layout文件填充的View会被直接添加进parent,如果我们使用这种方式后,在执行addView()方法就会造成上面的错误。

    推荐使用第四种方式 inflater.inflate(R.layout.item, parent, false);

    以上是我对inflate方法的使用理解,有什么不对的地方,大家积极留言,我看到后会第一时间回复大家,最后祝大家每天都有个好心情。

    作者:石头1314
    链接:https://www.jianshu.com/p/c92243287793
    来源:简书
    著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 相关阅读:
    [To be translated] Nova:libvirt image 的生命周期
    Neutron 理解(5):Neutron 是如何向 Nova 虚机分配固定IP地址的 (How Neutron Allocates Fixed IPs to Nova Instance)
    Cinder 调试
    SSH 无密码访问其它机器 和 Windows 上 putty 无密码访问 Linux 机器
    Hadoop 数据库
    Hadoop 分布式文件系统
    C++ 在继承中虚函数、纯虚函数、普通函数,三者的区别
    Google Scholar 论文参考文献的自动生成
    Linux shell ${}简单用法
    C/C++ 获取目录下的文件列表信息
  • 原文地址:https://www.cnblogs.com/qinaidexin/p/11726296.html
Copyright © 2011-2022 走看看