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

作者:bigconvience 时间:2023-05-29 21:50:28 

本文实例讲述了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程序设计有所帮助。

标签:Android,LayoutInflater
0
投稿

猜你喜欢

  • 详解Android v1、v2、v3签名(小结)

    2022-07-01 16:56:44
  • Android常用的AlertDialog对话框及自定义对话框

    2021-05-31 05:09:51
  • C#9.0推出的4个新特性介绍

    2021-10-10 07:49:29
  • Android Studio安装配置方法图文教程

    2023-08-12 04:34:03
  • Java 如何将网络资源url转化为File文件

    2023-05-28 09:23:37
  • java判断String类型是否能转换为int的方法

    2022-08-17 23:45:52
  • SpringCloud Eureka搭建的方法步骤

    2021-10-10 11:03:29
  • C#中ref关键字的用法

    2022-07-17 21:30:47
  • C#中缓存的基本使用方法

    2023-02-23 13:47:44
  • C#笔记之EF Code First 数据模型 数据迁移

    2022-07-03 05:27:58
  • Android Flutter自适应瀑布流案例详解

    2023-05-31 17:44:21
  • 带你一文了解C#中的Expression

    2023-04-20 04:37:57
  • Java中一个for语句导致无穷大死循环的例子

    2022-12-17 08:13:33
  • Java环境变量的设置方法(图文教程)

    2023-01-02 17:51:42
  • Spring+SpringMVC+MyBatis深入学习及搭建(二)之MyBatis原始Dao开发和mapper代理开发

    2021-07-24 06:36:00
  • mybatis like模糊查询特殊字符报错转义处理方式

    2023-09-02 21:14:54
  • 浅谈Java list.remove( )方法需要注意的两个坑

    2023-02-01 06:08:44
  • Java date format时间格式化操作示例

    2021-10-28 19:12:24
  • Java实现简单台球游戏

    2022-06-28 23:55:59
  • intellij idea创建第一个动态web项目的步骤方法

    2023-04-16 15:47:20
  • asp之家 软件编程 m.aspxhome.com