zoukankan      html  css  js  c++  java
  • inflate的使用注意事项

    public View inflate (int resource, ViewGroup root, boolean attachToRoot)

    我们在使用这个方法时,要清楚原理,下面是这个方法的文档介绍:

    Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
    Parameters
    resource
    ID for an XML layout resource to load (e.g., R.layout.main_page)
    root
    Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
    attachToRoot
    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.
    Returns
    The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.

      意思就是:root参数表示,如果attachToRoot为true的话,那么root为这个resource布局的父布局。如果attachToRoot为false,那么root仅仅为打气的这个布局提供了一组LayoutParams,暂时可以理解为所有的布局都需要一个LayoutParams,所以必须提供.

    其原理目前我还没有完全理解,基本可以总结为,在Fragment的OnCreateView方法中,需要用这个 View view = inflater.inflate(R.layout.my_fragment_layout, container, false);,因为Fragment的布局本身就是根布局

    在自定义ViewGroup中,要用这个

    //把生成的布局当做子类加入到当前控件中
    //this表示当前这个自定义ViewGroup
    View view = LayoutInflater.from(context).inflate(R.layout.mapmodel_relat_view, this, true);
  • 相关阅读:
    计算机网络中的多路复用技术
    ActiveMQ之一--ActiveMQ入门
    ehcache介绍
    I/O模型之二:Linux IO模式及 select、poll、epoll详解
    【甘道夫】HBase(0.96以上版本号)过滤器Filter具体解释及实例代码
    Android Studio安装及主题字体配置
    HDU 2136 Largest prime factor 參考代码
    update更新两个字段
    Hadoop对小文件的解决方式
    赵雅智_ContentProvider
  • 原文地址:https://www.cnblogs.com/android-yus/p/5436124.html
Copyright © 2011-2022 走看看