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);
  • 相关阅读:
    [LeetCode] Binary Tree Preorder Traversal
    Linux下搭建JSP环境
    Linux系统捕获数据包流程
    如何使用Linux套接字?
    IPod在Linux下的实战
    将手机流氓软件彻底赶出去
    如何修复和检测Windows系统漏洞
    防止网络渗透措施两则
    Cisco安全防护读书笔记之一Cisco系统设备协议漏洞
    至顶网推荐-Rpm另类用法加固Linux安全
  • 原文地址:https://www.cnblogs.com/android-yus/p/5436124.html
Copyright © 2011-2022 走看看