• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > 获取LayoutInflater对象的方法和inflate方法的一些参数问题,inflater.inflate参数

获取LayoutInflater对象的方法和inflate方法的一些参数问题,inflater.inflate参数

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了获取LayoutInflater对象的方法和inflate方法的一些参数问题,inflater.inflate参数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

获取LayoutInflater对象的方法和inflate方法的一些参数问题,inflater.inflate参数


一、获取LayoutInflater的三种方法

1、

LayoutInflater layoutInflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

2、

LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);

3、

LayoutInflater layoutInflater = MainActivity.this.getLayoutInflater();

其实查看它们的源码就会发现,后两种方法最终也还是调用第一种方法的context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)。不过查看第三种方法的源码要稍微绕一下,因为activity.getLayoutInflater(),其实是调用Window类的getLayoutInflater(),而这个是抽象类。根据这个类的注释,可以查看它的子类PhoneWindow,而这个是内部类,用开发工具是找不到这个类的,要手动去找。可以从sdk的文件目录下找,例如,我的是“D:\Android\Sdk\sources\android-23\com\android\internal\policy”。也可以直接在sdk目录下,搜索该类就行了。

其实View有一个静态的inflate方法,连LayoutInflater对象都帮你内部创建了。其方法源码如下:

    public static View inflate(Context context, @LayoutRes int resource, ViewGroup root) {
        LayoutInflater factory = LayoutInflater.from(context);
        return factory.inflate(resource, root);
    }

 

 二、inflate方法的一些参数问题

以前是困惑于inflate方法的作用。现在是困惑于什么时候才传入ViewGroup对象和attachToRoot的取值。所以分析了一下源码。

当写ListView的Adapter时,总要在getView方法里调用inflate方法,例如:

convertView = LayoutInflater.from(mContext).inflate(R.layout.push_to_refresh_header, parent, false);

这个inflate方法的源码是:

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                    + Integer.toHexString(resource) + ")");
        }

        final XmlResourceParser parser = res.getLayout(resource);
        try {
            return inflate(parser, root, attachToRoot);
        } finally {
            parser.close();
        }
    }

这个时候传入的ViewGroup对象不为nullt,同时设置boolean attachToRoot为false。

 

而在动态添加布局的时候,我学到的是下面这种写法:

View refreshView = layoutInflater.inflate(R.layout.push_to_refresh_header, null);
linearLayout.addView(refreshView);

这个inflate方法的源码是:

    public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

这个时候传入的ViewGroup对象为null。

 

可以看到,当调用两个参数的inflate方法时,它会以ViewGroup对象不等于空的判断值为参数attachToRoot的值,并和之前的参数传到另一个带有三个参数的inflate方法。

最终都会调用下面这个inflate方法。这里我们只需关注ViewGroup root、boolean attachToRoot和返回值result就可以了,所以我把其它省略了。

  1     public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
  2         synchronized (mConstructorArgs) {
         ......  
 9
View result = root; 10 11 try {            ......
32
33 if (TAG_MERGE.equals(name)) {               ......
40
} else { 41 // Temp is the root view that was found in the xml 42 final View temp = createViewFromTag(root, name, inflaterContext, attrs); 43 44 ViewGroup.LayoutParams params = null; 45 46 if (root != null) {                 ......
51
// Create layout params that match root, if supplied 52 params = root.generateLayoutParams(attrs); 53 if (!attachToRoot) { 54 // Set the layout params for temp if we are not 55 // attaching. (If we are, we use addView, below) 56 temp.setLayoutParams(params); 57 } 58 }               ......
70
71 // We are supposed to attach all the views we found (int temp) 72 // to root. Do that now. 73 if (root != null && attachToRoot) { 74 root.addView(temp, params); 75 } 76 77 // Decide whether to return the root that was passed in or the 78 // top view found in xml. 79 if (root == null || !attachToRoot) { 80 result = temp; 81 } 82 } 83 84 }
         ......
102 return result; 103 } 104 }

在第9行可以看到返回值result的初始值是root。

然后整个方法就只有第79行的if语句会修改result。而条件就是root为null,或者attachToRoot为假。而这个temp在第42行可以找到初值。其实上面的注释已经告诉我们,temp是我们传入的布局文件中的根视图。

在Adapter的getView方法里,虽然传入的root不为null,但attachToRoot为false,所以返回值就是我们传入的布局文件中的根视图,用来为布局中的控件初始化和修改。而上面那种动态添加布局的方式,传入的root为null,也是如此。

那为什么getView方法不直接传进null呢?这个我没能从源码上找到为什么,只知道,当传进ViewGroup对象为null时,运行会报错,说Adapter为null,就是Adapter对象没有初始化。那如果传进了root,但令attachToRoot为true呢?也是会运行报错。

先看看第73行的if语句。它表示的是root不为null,同时attachToRoot为true时,就会把temp和它的布局参数添加到root中。

所以在上面那种情况中,root其实就是ListView,而在ListView的父类AdapterView中,其方法的注释声明了是不支持addView方法的,一旦调用就会报错。

而我上面动态添加布局的那种写法,也

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • 获取LayoutInflater对象的方法和inflate方法的一些参数问题,inflater.inflate参数

相关文章

  • 2017-05-26Android:应用宝省流量更新
  • 2017-05-26Android中AsyncTask基本用法与源码剖析(API 23)
  • 2017-05-26Android开发通用的工具类
  • 2017-05-26安卓开源项目周报0411,安卓开源项目0411
  • 2017-05-26Android5 Zygote 与 SystemServer 启动流程分析
  • 2017-05-26更简单更全的material design状态栏
  • 2017-05-26Android 接入 OpenCV库的三种方式,androidopencv
  • 2017-11-18android 时间工具类
  • 2017-05-26For each循环中使用remove方法。,eachremove
  • 2017-05-26Android开发学习——ListView+BaseAdapter的使用,androidbaseadapter

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • Android Doze模式启用和恢复,androiddoze
    • Android新手入门2016(16)--画图
    • 注册时获取验证码常用的倒计时工具,注册时验证码
    • Android如何使用Https
    • 再谈Android AsyncTask的优缺点,androidasynctask
    • HTPC+NAS+ROUTER(wifi)的实现
    • 修改Android系统关机动画,android关机动画
    • 改变Activity启动时的默认动画,activity默认动画
    • 安卓第十五天笔记-图形图像一些简单处理,安卓第图形图像
    • Android中TextView设置最大长度,超出显示省略号,androidtextview

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有