Android LayoutInflater的用法详解

2023-10-22 13:50

本文主要是介绍Android LayoutInflater的用法详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

相信我们在开发过程中肯定接触过LayoutInflater,比如ListView的适配器里的getView方法里通过LayoutInflater.from(Context).inflater来加载xml布局,在Fragment里的onCreateView里面也是一样,加载布局一共三种方法。
1,在Activity里面调用getLayoutInflater()
2, 通过LayoutInflater.from(context).inflater()
3, context.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
以上的三种方式从实现上都是一样的,Activity里面的getLayoutInflater()实际上调用的是PhoneWindow的实现,而PhoneWindow里源码的处理是LayoutInflater.from(context).inflater(),往下查找最终调用context.getSystemService。
context.getSystemService是Android里一个比较重要的api,是Activity的一个方法,根据传入的Name来取得对应的Object,然后转换成相应的服务对象。以下是系统相应的服务。
传入的Name返回的对象说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务

但是LayoutInflater.from(context).inflater()的方法这么多,那它们到底是什么样的用法呢?

/**
341     * Inflate a new view hierarchy from the specified xml resource. Throws
342     * {@link InflateException} if there is an error.
343     *
344     * @param resource ID for an XML layout resource to load (e.g.,
345     *        <code>R.layout.main_page</code>)
346     * @param root Optional view to be the parent of the generated hierarchy.
347     * @return The root View of the inflated hierarchy. If root was supplied,
348     *         this is the root View; otherwise it is the root of the inflated
349     *         XML file.
350     */
351    public View inflate(int resource, ViewGroup root) {
352        return inflate(resource, root, root != null);
353    }
354
355    /**
356     * Inflate a new view hierarchy from the specified xml node. Throws
357     * {@link InflateException} if there is an error. *
358     * <p>
359     * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
360     * reasons, view inflation relies heavily on pre-processing of XML files
361     * that is done at build time. Therefore, it is not currently possible to
362     * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
363     *
364     * @param parser XML dom node containing the description of the view
365     *        hierarchy.
366     * @param root Optional view to be the parent of the generated hierarchy.
367     * @return The root View of the inflated hierarchy. If root was supplied,
368     *         this is the root View; otherwise it is the root of the inflated
369     *         XML file.
370     */
371    public View inflate(XmlPullParser parser, ViewGroup root) {
372        return inflate(parser, root, root != null);
373    }
374
375    /**
376     * Inflate a new view hierarchy from the specified xml resource. Throws
377     * {@link InflateException} if there is an error.
378     *
379     * @param resource ID for an XML layout resource to load (e.g.,
380     *        <code>R.layout.main_page</code>)
381     * @param root Optional view to be the parent of the generated hierarchy (if
382     *        <em>attachToRoot</em> is true), or else simply an object that
383     *        provides a set of LayoutParams values for root of the returned
384     *        hierarchy (if <em>attachToRoot</em> is false.)
385     * @param attachToRoot Whether the inflated hierarchy should be attached to
386     *        the root parameter? If false, root is only used to create the
387     *        correct subclass of LayoutParams for the root view in the XML.
388     * @return The root View of the inflated hierarchy. If root was supplied and
389     *         attachToRoot is true, this is root; otherwise it is the root of
390     *         the inflated XML file.
391     */
392    public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
393        if (DEBUG) System.out.println("INFLATING from resource: " + resource);
394        XmlResourceParser parser = getContext().getResources().getLayout(resource);
395        try {
396            return inflate(parser, root, attachToRoot);
397        } finally {
398            parser.close();
399        }
400    }

上面的方法非常清晰,直接看下inflate(parser, root, attachToRoot);

/**
403     * Inflate a new view hierarchy from the specified XML node. Throws
404     * {@link InflateException} if there is an error.
405     * <p>
406     * <em><strong>Important</strong></em>&nbsp;&nbsp;&nbsp;For performance
407     * reasons, view inflation relies heavily on pre-processing of XML files
408     * that is done at build time. Therefore, it is not currently possible to
409     * use LayoutInflater with an XmlPullParser over a plain XML file at runtime.
410     *
411     * @param parser XML dom node containing the description of the view
412     *        hierarchy.
413     * @param root Optional view to be the parent of the generated hierarchy (if
414     *        <em>attachToRoot</em> is true), or else simply an object that
415     *        provides a set of LayoutParams values for root of the returned
416     *        hierarchy (if <em>attachToRoot</em> is false.)
417     * @param attachToRoot Whether the inflated hierarchy should be attached to
418     *        the root parameter? If false, root is only used to create the
419     *        correct subclass of LayoutParams for the root view in the XML.
420     * @return The root View of the inflated hierarchy. If root was supplied and
421     *         attachToRoot is true, this is root; otherwise it is the root of
422     *         the inflated XML file.
423     */
424    public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {
425        synchronized (mConstructorArgs) {
426            final AttributeSet attrs = Xml.asAttributeSet(parser);
427            Context lastContext = (Context)mConstructorArgs[0];
428            mConstructorArgs[0] = mContext;
429            View result = root;
430
431            try {
432                // Look for the root node.
433                int type;
434                while ((type = parser.next()) != XmlPullParser.START_TAG &&
435                        type != XmlPullParser.END_DOCUMENT) {
436                    // Empty
437                }
438
439                if (type != XmlPullParser.START_TAG) {
440                    throw new InflateException(parser.getPositionDescription()
441                            + ": No start tag found!");
442                }
443
444                final String name = parser.getName();
445
446                if (DEBUG) {
447                    System.out.println("**************************");
448                    System.out.println("Creating root view: "
449                            + name);
450                    System.out.println("**************************");
451                }
452
453                if (TAG_MERGE.equals(name)) {
454                    if (root == null || !attachToRoot) {
455                        throw new InflateException("<merge /> can be used only with a valid "
456                                + "ViewGroup root and attachToRoot=true");
457                    }
458
459                    rInflate(parser, root, attrs, false);
460                } else {
461                    // Temp is the root view that was found in the xml
462                    View temp;
463                    if (TAG_1995.equals(name)) {
464                        temp = new BlinkLayout(mContext, attrs);
465                    } else {
466                        temp = createViewFromTag(root, name, attrs);
467                    }
468
469                    ViewGroup.LayoutParams params = null;
470                    // 当父层不为空时
471                    if (root != null) {
472                        if (DEBUG) {
473                            System.out.println("Creating params from root: " +
474                                    root);
475                        }
476                        // Create layout params that match root, if supplied// 获取父层的参数并赋值
477                        params = root.generateLayoutParams(attrs);//  子布局不贴上也会被设置LayoutParams
478                        if (!attachToRoot) {
479                            // Set the layout params for temp if we are not
480                            // attaching. (If we are, we use addView, below)
481                            temp.setLayoutParams(params);
482                        }
483                    }
484
485                    if (DEBUG) {
486                        System.out.println("-----> start inflating children");
487                    }
488                    // Inflate all children under temp
489                    rInflate(parser, temp, attrs, true);
490                    if (DEBUG) {
491                        System.out.println("-----> done inflating children");
492                    }
493
494                    // We are supposed to attach all the views we found (int temp)
495                    // to root. Do that now.// 父布局不为空并且贴上会被add到父层
496                    if (root != null && attachToRoot) {
497                        root.addView(temp, params);
498                    }
499
500                    // Decide whether to return the root that was passed in or the
501                    // top view found in xml.// 父布局为空或者没有贴上,result就是View本身
502                    if (root == null || !attachToRoot) {
503                        result = temp;
504                    }
505                }
506
507            } catch (XmlPullParserException e) {
508                InflateException ex = new InflateException(e.getMessage());
509                ex.initCause(e);
510                throw ex;
511            } catch (IOException e) {
512                InflateException ex = new InflateException(
513                        parser.getPositionDescription()
514                        + ": " + e.getMessage());
515                ex.initCause(e);
516                throw ex;
517            } finally {
518                // Don't retain static reference on context.
519                mConstructorArgs[0] = lastContext;
520                mConstructorArgs[1] = null;
521            }
522          // 最后返回 result
523            return result;
524        }
525    }

由上源码我们可以得出:
inflate(layout, null)返回的是View本身,addView后View本身所设置的布局参数无效,由父层和子View决定大小。
inflate(layout, null, false)同上一样,当父层为空,第三个值是否为真没有意义。
inflate(layout, parent)子布局会被add到父层并为该View设置布局参数,具体大小由父层和子View决定。
inflate(layout, parent, false)同上一样,区别就是false父层不会addView。
inflate(layout, parent, true)同第三个方法一样,父层View会addView并为其设置参数。

这篇关于Android LayoutInflater的用法详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/261845

相关文章

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

C#数据结构之字符串(string)详解

《C#数据结构之字符串(string)详解》:本文主要介绍C#数据结构之字符串(string),具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录转义字符序列字符串的创建字符串的声明null字符串与空字符串重复单字符字符串的构造字符串的属性和常用方法属性常用方法总结摘

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Java进行文件格式校验的方案详解

《Java进行文件格式校验的方案详解》这篇文章主要为大家详细介绍了Java中进行文件格式校验的相关方案,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、背景异常现象原因排查用户的无心之过二、解决方案Magandroidic Number判断主流检测库对比Tika的使用区分zip

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

java之Objects.nonNull用法代码解读

《java之Objects.nonNull用法代码解读》:本文主要介绍java之Objects.nonNull用法代码,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录Java之Objects.nonwww.chinasem.cnNull用法代码Objects.nonN

Python中随机休眠技术原理与应用详解

《Python中随机休眠技术原理与应用详解》在编程中,让程序暂停执行特定时间是常见需求,当需要引入不确定性时,随机休眠就成为关键技巧,下面我们就来看看Python中随机休眠技术的具体实现与应用吧... 目录引言一、实现原理与基础方法1.1 核心函数解析1.2 基础实现模板1.3 整数版实现二、典型应用场景2

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML