关于inf

方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前连个参数都好理解,我比较费解的是第3个参数。

文档中的解释是: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.
 
举个例子看一下
新建一个工程
 
工程包含两个xml文件
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
   
    <FrameLayout
        android:id="@+id/ffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></FrameLayout>
 
</LinearLayout>
 
layout/ffff.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
 
</LinearLayout>
 
接下来看activity中怎么写的
这里分3中情况
first, no attachToRoot params
activity 中的部分代码,注意看红色部分
        setContentView(R.layout.main);
        ViewGroup v =
(ViewGroup) findViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v);
 

 
布局结构图

 
Second, params attachToRoot is false
View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
 

发现没有了ffff.xml 中的内容
通过结构图查看,确实没有了

 
Third,
        ViewGroup v =
(ViewGroup) findViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
        v.addView(vv);
 
运行结果

 
呵呵,又有了。
 
所以这个参数的作用就是,是否把选取的视图加入到root中。false 的意思就是不添加到root中。可能需要我们手动添加。

关于inflate的第3个参数的更多相关文章

  1. Android LayoutInflater.inflate使用上的问题解惑

    最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate ...

  2. [译转]深入理解LayoutInflater.inflate()

    原文链接:https://www.bignerdranch.com/blog/understanding-androids-layoutinflater-inflate/ 译文连接:http://bl ...

  3. RecyclerView使用大全

    RecylerView介绍 RecylerView是support-v7包中的新组件,是一个强大的滑动组件,与经典的ListView相比,同样拥有item回收复用的功能,这一点从它的名字recyler ...

  4. [Android]异步 layout inflation(翻译)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/5829809.html 异步 layout inflation ...

  5. android Fragments介绍

    Fragments是Android3.0引入的概念,译为片段.碎片,为了解决不同屏幕分辩率的动态和灵活UI设计. Fragment表现Activity中UI的一个行为或者一部分.可以将多个fragme ...

  6. 关于Fragment你所需知道的一切!

    转载自刘明渊 的博客地址:http://blog.csdn.net/vanpersie_9987 Fragment 是 Android API 中的一个类,它代表Activity中的一部分界面:您可以 ...

  7. 【Android UI】:Fragment官方文档

    概述   Fragment表现Activity中UI的一个行为或者一部分.可以将多个fragment组合在一起,放在一个单独的activity中来创建一个多界面区域的UI,并可以在多个activity ...

  8. Android入门(四)UI-创建自定义控件

    原文链接:http://www.orlion.ga/441/ 一.引入布局 iphone应用顶部会有一个标题栏,我们可以模仿着做一个,但是如果我们的程序中很多个活动都需要这样的标题栏,如果 每一个活动 ...

  9. Android学习笔记(九)——布局和控件的自定义

    //此系列博文是<第一行Android代码>的学习笔记,如有错漏,欢迎指正! View是 Android中一种最基本的 UI组件,它可以在屏幕上绘制一块矩形区域,并能响应这块区域的各种事件 ...

随机推荐

  1. shell中各种括号的作用详解()、(())、[]、[[]]、{}

    一.小括号,圆括号() 1.单小括号 () ①命令组.括号中的命令将会新开一个子shell顺序执行,所以括号中的变量不能够被脚本余下的部分使用.括号中多个命令之间用分号隔开,最后一个命令可以没有分号, ...

  2. spring源码学习之AOP(二)

    接着上一篇中的内容! 3.创建代理 在获取了所有的bean对应的增强器之后,便可以进行代理的创建了org.springframework.aop.framework.autoproxy包下的Abstr ...

  3. niec-validator 表单验证使用案例

    css .msg-box span { font-size: .5rem; color: #7699c6; } .msg-box .tip { padding-left: 18px; backgrou ...

  4. 组件化开发之vue

    今天写了写vue的组件化开发demo,有些小的心得.分享一下. 组件化意味着代码可以复用,调用组件就可以了.然后可以通过组件调用组件的相关能力. 例如以前我做组件化开发的一个小项目 原生js组件的实现 ...

  5. Clash Credenz 2014 Wild Card Round题解

    A题 简单模拟. /************************************************************************* > File Name: ...

  6. Codeforces 463D

    题目链接 D. Gargari and Permutations time limit per test 2 seconds memory limit per test 256 megabytes i ...

  7. applyMiddleware源码中的闭包

    闭包都是个老掉牙的话题了,这次又提起,是因为重看Redux源码时发现了applyMiddleware里的用法很巧妙.我们先看一个简单的例子. var a = (num) => num + 1 v ...

  8. socker TCP UDP BIO NIO

    BIO:  Java 1.4 以前只有之中方式. bio:阻塞式IO, 一个 socker 连接占用一个 线程.如果 IO 阻塞,会在传输速度限制,这个线程也会一直等待在这里,等待从socker 的 ...

  9. gin入门-1

    Gin框架介绍 1. 简介Gin框架介绍A. 基于httprouter开发的web框架.http://github.com/julienschmidt/httprouterB. 提供Martini风格 ...

  10. hdoj 1325 Is It A Tree? 【并查集】

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/shengweisong/article/details/34099151 做了一上午,最终ac了 w ...