原文出处:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。

具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式
1. LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater() 
2. LayoutInflater inflater = LayoutInflater.from(context);  
3. LayoutInflater inflater =  (LayoutInflater)context.getSystemService
                              (Context.LAYOUT_INFLATER_SERVICE);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context)
{   
 super(context);   
    mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(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;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
另外getSystemService()是Android很重要的一个API,它是Activity的一个方法,根据传入的NAME来取得对应的Object,然后转换成相应的服务对象。以下介绍系统相应的服务。
 
传入的Name  返回的对象 说明
WINDOW_SERVICE WindowManager 管理打开的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定义的view
ACTIVITY_SERVICE ActivityManager 管理应用程序的系统状态
POWER_SERVICE PowerManger 电源的服务
ALARM_SERVICE AlarmManager 闹钟的服务
NOTIFICATION_SERVICE NotificationManager 状态栏的服务
KEYGUARD_SERVICE KeyguardManager 键盘锁的服务
LOCATION_SERVICE LocationManager 位置的服务,如GPS
SEARCH_SERVICE SearchManager 搜索的服务
VEBRATOR_SERVICE Vebrator 手机震动的服务
CONNECTIVITY_SERVICE Connectivity 网络连接的服务
WIFI_SERVICE WifiManager Wi-Fi服务
TELEPHONY_SERVICE TeleponyManager 电话服务
inflate 方法
通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root) 
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)  
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
示意代码:
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);       
View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));       
//EditText editText = (EditText)findViewById(R.id.content);// error 
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate 方法与 findViewById 方法不同;
·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
 
public View inflate(int Resourece,ViewGrouproot) 
作用:填充一个新的视图层次结构从指定的XML资源文件中 
reSource:View的layout的ID 
root: 生成的层次结构的根视图 
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。 

android LayoutInflater和inflate()方法的用法(转载)的更多相关文章

  1. 【Android 界面效果43】Android LayoutInflater的inflate方法中attachToRoot的作用

    我们在ListView的Adapter的getView方法里面经常会调用两个参数的inflate方法, mInflater.inflate(R.layout.adv_viewpager, null); ...

  2. LayoutInflater和inflate()方法的用法

    LayoutInflater作用是将layout的xml布局文件实例化为View类对象. 实现LayoutInflater的实例化共有3种方法, (1).通过SystemService获得 Layou ...

  3. Android编程之LayoutInflater的inflate方法实例

    假设你不关心其内部实现,仅仅看怎样使用的话,直接看这篇就可以. 接上篇,接下来,就用最最简单的样例来说明一下: 用两个布局文件main 和 test: 当中,main.xml文件为: <?xml ...

  4. Android编程之LayoutInflater的inflate方法详解

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到: public View onCreateView(LayoutInflater infl ...

  5. Android编程之LayoutInflater的inflate方法具体解释

    LayoutInflater的inflate方法,在fragment的onCreateView方法中经经常使用到: public View onCreateView(LayoutInflater in ...

  6. 三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

  7. 带你看懂LayoutInflater中inflate方法

    关于inflate问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东 ...

  8. View.inflate和LayoutInflater的inflate方法区别

    平时ListView加载item中,adapter的getView方法中,我们经常用到: LayoutInflater.from(mContext).inflate(R.layout.it ,pare ...

  9. 【转】三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

    关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个 ...

随机推荐

  1. 浅谈String类型

    首先,我们要知道的是String类型是一个引用类型,它的基类是Object.并且它的内容是只读的. 我们有时候经常会看到两个字符串类型,一个是“Sting”,一个是“string”.大写的String ...

  2. mysql子查询优化

    ,,,) ) LIMIT 第一种方式in where:2000ms SELECT COUNT(*) AS tp_count FROM xxx_b2c_orders o ,,,) and from xx ...

  3. CLR.via.C#第三版 读书笔记

    第一章 CLR的执行模型 1.1将源代码编译成托管代码 决定将.NET Framework作为自己的开发平台之后,第一步是决定要生成什么类型的应用程序或组件.假定你已经完成了这些次要的细节:一切都已经 ...

  4. JS 实现取整(二)

    1.直接丢弃小数部分,保留整数部分 a:parseInt(1.5555) b: 0|1.5555 2.向上取整 a: Math.ceil(1.5555) b: (1.5555+0.5).toFixed ...

  5. EXTJS 4.2 日期控件

    { xtype: "fieldcontainer", layout: "hbox", items: [{ fieldLabel: '开始时间', name: ' ...

  6. Xcode文档下载与安装路径

    https://developer.apple.com/library/downloads/docset-index.dvtdownloadableindex ~/Library/Developer/ ...

  7. Jquery异步请求简单实例(转)

    本文引用自Xingsoft. 一.Jquery向aspx页面请求数据 前台页面JS代码:             $("#Button1").bind("click&qu ...

  8. swift基础--运算符

    (1)加减乘除 (2)三目运算,切记后面的空格如果不加会报错的.估计是苹果的bug. (3)聚合运算符,省却了一个判断,很人性化 (4)区间运算符 //加减乘除等等 let a = 2 let b = ...

  9. SpringMVC注解@RequestParam(转)

    鸣谢:http://shawnccx.iteye.com/blog/730239 -------------------------------------------------- 在SpringM ...

  10. @Repository、@Service、@Controller 和 @Component(转)

    鸣谢:http://blog.csdn.net/ye1992/article/details/19971467 @Repository.@Service.@Controller 和 @Componen ...