Android编程之LayoutInflater的inflate方法详解

LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:

    public View inflate(int resource, ViewGroup root) {
        return inflate(resource, root, root != null);
    }
    public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
        XmlResourceParser parser = getContext().getResources().getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

所以,这里直接介绍里面调用这个方法即可:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)

在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:

                    ViewGroup.LayoutParams params = null;

                    if (root != null) {
                        if (DEBUG) {
                            System.out.println("Creating params from root: " +
                                    root);
                        }
                        // Create layout params that match root, if supplied
                        params = root.generateLayoutParams(attrs);
                        if (!attachToRoot) {
                            // Set the layout params for temp if we are not
                            // attaching. (If we are, we use addView, below)
                            temp.setLayoutParams(params);
                        }
                    }

params = root.generateLayoutParams(attrs);

这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。

接着往下看:

                    // We are supposed to attach all the views we found (int temp)
                    // to root. Do that now.
                    if (root != null && attachToRoot) {
                        root.addView(temp, params);
                    }

                    // Decide whether to return the root that was passed in or the
                    // top view found in xml.
                    if (root == null || !attachToRoot) {
                        result = temp;
                    }

root.addView(temp, params);

如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。

如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。

以上就是该方法内容,可能你还有点看不太懂吧,下一篇文章,我将会做几个例子,来具体说明一下。

时间: 2024-07-30 03:25:17

Android编程之LayoutInflater的inflate方法详解的相关文章

Android编程之LayoutInflater的inflate方法实例

假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo

linux网络编程之shutdown() 与 close()函数详解

linux网络编程之shutdown() 与 close()函数详解 参考TCPIP网络编程和UNP: shutdown函数不能关闭套接字,只能关闭输入和输出流,然后发送EOF,假设套接字为A,那么这个函数会关闭所有和A相关的套接字,包括复制的:而close能直接关闭套接字. 1.close()函数 [cpp] view plain copy print? <span style="font-size:13px;">#include<unistd.h> int 

网络编程之TCP/IP各层详解

网络编程之TCP/IP各层详解 我们将应用层,表示层,会话层并作应用层,从TCP/IP五层协议的角度来阐述每层的由来与功能,搞清楚了每层的主要协议,就理解了整个物联网通信的原理. 首先,用户感知到的只是最上面一层--应用层,自上而下每层都依赖于下一层,所以我们从最下层开始切入,比较好理解. 每层都运行特定的协议,越往上越靠近用户,越往下越靠近硬件. 一.物理层 由来:孤立的计算机之间要一起"玩耍",就必须接入Internet,即计算机之间必须完成组网. 物理层功能:主要是基于电器特性发

Android layoutInflate.inflate 方法详解,removeView()错误解决

错误: The specified child already has a parent. You must call removeView(). 解答: 这个错误很直白,就是你viewGroup.addView(childView); 中childView已经有父View了.错误原因很多,我主要讲下 mLayoutInflater.inflate(id, rootView, false);造成的这个错误.(该方法有两种,一种是2个参数,一种是3个参数). 2个参数: 第一个参数:layout的

Shell编程之Expect免交互语句详解

Expect概述 Expect是建立在tcl基础_上的一个工具,Expect 是用来进行自动化控制和测试的工具.主要解决shell脚本中不可交互的问题.对于大规模的linux运维很有帮助.在linux运维和开发中,我们经常需要远程登录服务器进行操作,登录的过程是一个交互的过程,可能会需要输入(yes/no)password等信息.为了模拟这种输入,可以使用Expect脚本. Expect安装 yum install expect -y 基本命令 send: 向进程发送字符串,用于模拟用户的输入.

Android编程之Fragment动画加载方法源码详解

上次谈到了Fragment动画加载的异常问题,今天再聊聊它的动画加载loadAnimation的实现源代码: Animation loadAnimation(Fragment fragment, int transit, boolean enter, int transitionStyle) { 接下来具体看一下里面的源码部分,我将一部分一部分的讲解,首先是: Animation animObj = fragment.onCreateAnimation(transit, enter, fragm

[Android]LayoutInflater的inflate方法半详解

好久没写博客,作为一名刚加入android阵营的android狗,发心得刷人气来了!!!(半详解是因为说详不那么详,说不详也稍微有点详..)哈哈~~..咳..咳.. 一.Activity中的setContentView 对于刚开始学Android的新手来说,在Activity中加载布局文件的方法是在onCreate()回调方法中直接调用setContentView()方法,如: @Override protected void onCreate(Bundle savedInstanceState

三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

版权声明:本文为sang原创文章,转载请注明出处. 目录(?)[+] 关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东.本篇博客我们不讲源码,只看使用.源码的解读会在下一篇博文中带来. inflate方法从大范围来看,分两种,三个参数的构造方法和两个参数的构造方法.在这两类中又有细分,OK,那我们就把各种情况都来演示一遍. 1.三个参数的in

Android编程之Fragment使用动画造成Unknown animation name: objectAnimator异常

在为Fragment做切换动画,启动后遇到了一个异常: Caused by: java.lang.RuntimeException: Unknown animation name: objectAnimator 截图如下: 我的代码如下: fragment = Fragment.instantiate(getActivity(), clz.getName()); fragment.setArguments(args); ft.setCustomAnimations(R.animator.frag