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

本文实例讲述了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开发入门与进阶教程》、《Android控件用法总结》、《Android短信与电话操作技巧汇总》及《Android多媒体操作技巧汇总(音频,视频,录音等)》

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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索android
layoutinflater
layoutinflater获取、layoutinflater、layoutinflater用法、layoutinflater.from、getlayoutinflater,以便于您获取更多的相关知识。

时间: 2024-09-29 08:58:33

Android开发之获取LayoutInflater对象的方法总结_Android的相关文章

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

本文实例讲述了Android开发之获取LayoutInflater对象的方法.分享给大家供大家参考,具体如下: 在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View.本文主要目的是总结获取LayoutInflater对象的方法. 1.若能获取context对象,可以有以下几种方法: LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYO

Android开发中Intent传递对象的方法分析_Android

本文实例分析了Android开发中Intent传递对象的方法.分享给大家供大家参考,具体如下: 方法一: 利用方法:public Intent putExtra (String name, Parcelable value)传递一个Parceable的参数,此方法的参数被序列化到内存. 利用方法:public Intent putExtra (String name, Serializable value)传递一个实现了序列化接口类的对象,此方法的实参被序列化到磁盘. 方法二: 把数据存放到应用

Android开发中Intent传递对象的方法分析

本文实例分析了Android开发中Intent传递对象的方法.分享给大家供大家参考,具体如下: 方法一: 利用方法:public Intent putExtra (String name, Parcelable value)传递一个Parceable的参数,此方法的参数被序列化到内存. 利用方法:public Intent putExtra (String name, Serializable value)传递一个实现了序列化接口类的对象,此方法的实参被序列化到磁盘. 方法二: 把数据存放到应用

Android编程使用Intent传递对象的方法分析_Android

本文实例分析了Android编程使用Intent传递对象的方法.分享给大家供大家参考,具体如下: 之前的文章中,介绍过Intent的用法,比如启动活动,发送广播,启发服务等,并且可以使用Intent时传递一些数据.如下代码所示: Intent intent = new Intent(this,SecondActivity.class); intent.putExtra("info", "I am fine"); startActivity(intent); 在传递数

Android开发进阶自定义控件之滑动开关实现方法【附demo源码下载】_Android

本文实例讲述了Android开发进阶自定义控件之滑动开关实现方法.分享给大家供大家参考,具体如下: 自定义开关控件 Android自定义控件一般有三种方式 1.继承Android固有的控件,在Android原生控件的基础上,进行添加功能和逻辑. 2.继承ViewGroup,这类自定义控件是可以往自己的布局里面添加其他的子控件的. 3.继承View,这类自定义控件没有跟原生的控件有太多的相似的地方,也不需要在自己的肚子里添加其他的子控件. ToggleView自定义开关控件表征上没有跟Androi

Android开发之简单文件管理器实现方法_Android

本文实例讲述了Android开发之简单文件管理器实现方法.分享给大家供大家参考,具体如下: 这里运用Java I/O.ListActivity.Dialog.Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件.比较简单,直接看代码: 先看布局文件: layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&q

Android开发之简单文件管理器实现方法

本文实例讲述了Android开发之简单文件管理器实现方法.分享给大家供大家参考,具体如下: 这里运用Java I/O.ListActivity.Dialog.Bitmap等实现简单文件管理器,可以查看目录文件,修改文件名,删除文件,打开文件.比较简单,直接看代码: 先看布局文件: layout/main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&q

Android开发之加载图片的方法_Android

本文实例讲述了Android开发之加载图片的方法.分享给大家供大家参考.具体分析如下: 加载网络上的图片需要在manifest中配置访问网络的权限,如下: <uses-permission android:name="android.permission.INTERNET" /> 如果不配置这个权限的话,会报错:unknown host exception. package com.example.loadimgfromweb; import java.io.InputSt

Android使用getIdentifier()获取资源Id的方法_Android

本文实例讲述了Android使用getIdentifier()获取资源Id的方法.分享给大家供大家参考,具体如下: int i= getResources().getIdentifier("icon", "drawable", getPackageName()) ; if(i>0) {Log.i("aa","aa");} else {Log.i("vbv","aa");} 或者: