Android开发之获取LayoutInflater对象的方法总结
作者:bigconvience 发布时间:2023-05-29 21:50:28
标签:Android,LayoutInflater
本文实例讲述了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实现源码如下:
/**
* Inflate a view from an XML resource. This convenience method wraps the {@link
* LayoutInflater} class, which provides a full range of options for view inflation.
*
* @param context The Context object for your activity or application.
* @param resource The resource ID to inflate
* @param root A view group that will be the parent. Used to properly inflate the
* layout_* parameters.
* @see LayoutInflater
*/
public static View inflate(Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
}
LayoutInflater.from(context)实际上是对方法1的包装,可参考以下源码:
/**
* Obtains the LayoutInflater from the given context.
*/
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程序设计有所帮助。


猜你喜欢
- 前言表之间的关系有几种:一对多、多对一、 一对一、多对多在多对一关系中,把多的部分拆成一个一个对象其实就是一对一关系,如账户和用户是多对一关
- 一.前言Unity3D是如今最火爆的游戏开发引擎,它可以让我们能轻松创建诸如三维视频游戏、建筑可视化、实时三维动画等类型的互动内容。它支持2
- 场景:假设每次我们去超市购物,我们都会推一个购物车,有水果、蔬菜、肉类三种商品,提供给我们选择,那么这时候,如果使用装饰器模式,应该如何实现
- 目录Java 中线程池创建的几种方式🐱🏍Executors 工厂方法创建👏 new ThreadPoolExecutor() 自
- 单个字符分割 string s="abcdeabcdeabcde"; string[]
- 1.预警需求为了更好的管理商品日期,需要对仓库的商品进行预警管理,对商品的保质期控制在一个范围内提示出来,也可以通过该功能间接的展示出一个商
- web采集的数据为 %u6B63%u5F0F%u4EBA%u5458,需要读取并转换为python对象,想了下不调用Javascript去e
- 本文实例为大家分享了Android实现单选按钮的具体代码,供大家参考,具体内容如下单选按钮在默认情况下,单选按钮显示为一个圆形图标,可以在图
- file.mkdir()创建单级文件夹,file.mkdirs()创建多级文件夹,file.createNewFile()创建的是一个文件。
- Unicode有四种编码格式,UTF-8, UTF-16,UTF-32,UTF-7。字符编码类,ASCIIEncoding ,UTF7Enc
- 下文笔者讲述maven引入本地jar包时,运行报错"java.lang.NoClassDefFoundError"的处理
- 简介单例指的是只能存在一个实例的类(在C#中,更准确的说法是在每个AppDomain之中只能存在一个实例的类,它是软件工程中使用最多的几种模
- servlet实现文件上传,预览,下载和删除,供大家参考,具体内容如下一、准备工作:1.1 文件上传插件:uploadify;1.2 文件上
- 前言此文章主要解决拦截用户点击手机底部导航栏中的返回键时该事件的拦截;此方法依然可以适用于fragmentonBackPressed()这是
- 描述:由于产品需求,要求含有EditText的界面全屏显示,最好的解决方式是使用AndroidBug5497Workaround.assis
- 本文研究的主要是Java中hashCode的正确求值方法的相关内容,具体如下。散列表有一项优化,可以将对象的散列码(hashCode)缓存起
- 一、申请你的AppIDhttp://open.weixin.qq.com/ 友情提示:推荐使用eclipse打包软件最后一步的M
- 题目给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。示例 1:输入: 123输出: 321 示例
- @EventListener 异步中使用condition的问题@EventListener是spring在4.2+推出的更好的使用spri
- 本文实例为大家分享了C#获取计算机信息的具体代码,供大家参考,具体内容如下using System;using System.Configu