Android LayoutInflater是我们在Android App开发中特别是自定义view时候经常常用的一个类。也许你开发中没有特别注意到它,但其实我们都直接或间接的使用它了。这里主要说明Android LayoutInflater的获取方式及其inflate()参数和返回值的关系。

LayoutInflater获取

  • LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  • LayoutInflater layoutInflater = LayoutInflater.from(Context context)
  • LayoutInflater layoutInflater = activity.getLayoutInflater();
    后面2种方式最后都还是调用到getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
    而LayoutInflater 是一个抽象类,真正实现的是policy底下的PhoneLayoutInflater。
    PhoneLayoutInflater是在SystemServiceRegistry.java注册到SystemService中的:
registerService(Context.LAYOUT_INFLATER_SERVICE, LayoutInflater.class,                new CachedServiceFetcher() {            @Override            public LayoutInflater createService(ContextImpl ctx) {                return new PhoneLayoutInflater(ctx.getOuterContext());            }});

LayoutInflater.inflate()方法

  • View inflate(@LayoutRes int resource, @Nullable ViewGroup root)
  • View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
  • View inflate(XmlPullParser parser, @Nullable ViewGroup root)
  • View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot)
    其中,关于XmlPullParser 在API文档中有这样一段话:

Important   For performance
reasons, view inflation relies heavily on pre-processing of XML files
that is done at build time. Therefore, it is not currently possible to
use LayoutInflater with an XmlPullParser over a plain XML file at runtime.

大意是:基于性能考虑,Android的Layout文件在编译时候做了预处理;因此,在运行时给LayoutInflater传递一个未预处理的xml的XmlPullParser是行不通地。

LayoutInflater.inflate()的返回值

 * @param 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.) * @param 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. * @return 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.

说白了,inflate()方法的返回值取决于参数root和attachToRoot:

  • if(root!=null && attachToRoo) return root;
  • else return root of the inflated XML

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. Python list sort方法的具体使用
  3. python list.sort()根据多个关键字排序的方法实现
  4. 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
  5. Android解析WindowManagerService(一)WMS的诞生
  6. Android中JNI的使用方法
  7. android关于activity的组成
  8. Android(安卓)Studio打包生成Jar包的方法(亲测可用)
  9. android屏幕旋转时,Activity不重新调用onCreate的方法

随机推荐

  1. Android 约束布局(ConstraintLayout)的使
  2. Android中简单实现选择图片并裁剪
  3. Android传奇
  4. 安卓课程六 android常用布局属性的介绍
  5. Android高手进阶教程(六)之----Android
  6. Android 新浪授权和分享经验总结
  7. android DataStorage
  8. 【 Android 10 四大组件 】系列 -- Activ
  9. android横竖屏切换、键盘推出状态改变的
  10. Android Unparsed aapt error(s)! Check