iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android开发之获取LayoutInflater对象的方法总结
  • 432
分享到

Android开发之获取LayoutInflater对象的方法总结

方法layoutinflaterandroid开发Android 2022-06-06 09:06:01 432人浏览 泡泡鱼
摘要

本文实例讲述了Android开发之获取LayoutInflater对象的方法。分享给大家供大家参考,具体如下: 在写Android程序时,有时候会编写自定义的View,使用In

本文实例讲述了Android开发之获取LayoutInflater对象的方法。分享给大家供大家参考,具体如下:

在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View。本文主要目的是总结获取LayoutInflater对象的方法。

1、若能获取context对象,可以有以下几种方法:


LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View child = inflater.inflate(R.layout.child, null);

或者:


LayoutInflater inflater = LayoutInflater.from(context);
View child = inflater.inflate(R.layout.child, null);

2、在一个Activity中,可以有以下方法:


View child = getLayoutInflater().inflate(R.layout.child, item, false);

或者:


View view;
LayoutInflater inflater = (LayoutInflater)  getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);

方法1和方法2其实都是对context().getSystemService()的使用

3、使用View的静态方法:


View view=View.inflate(context, R.layout.child, null)

inflate实现源码如下:



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

LayoutInflater.from(context)实际上是对方法1的包装,可参考以下源码:



public static LayoutInflater from(Context context) {
  LayoutInflater LayoutInflater =
      (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  if (LayoutInflater == null) {
    throw new AssertionError("LayoutInflater not found.");
  }
  return LayoutInflater;
}

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android控件用法总结》、《Android短信与电话操作技巧汇总》及《Android多媒体操作技巧汇总(音频,视频,录音等)》

希望本文所述对大家Android程序设计有所帮助。

您可能感兴趣的文章:android之自定义Toast使用方法超简单实现Android自定义Toast示例(附源码)Android 自定义 Toast 显示时间Android编程实现Toast自定义布局简单示例Android编程经典代码集锦(复制,粘贴,浏览器调用,Toast显示,自定义Dialog等)Android Toast自定义显示时间基于Android LayoutInflater的使用介绍Android LayoutInflater加载布局详解及实例代码Android布局加载之LayoutInflater示例详解Android开发实现自定义Toast、LayoutInflater使用其他布局示例


--结束END--

本文标题: Android开发之获取LayoutInflater对象的方法总结

本文链接: https://www.lsjlt.com/news/25715.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作