Android Activity探索之LayoutInflater详解

一、简介

  LayoutInflater与findViewById(int id)功能类似,主要用于查找res/layout/***下xml布局文件。而findViewById(int id)方法是查找已经加载的布局文件中具体widget控件,比如:Button,TextView等。具体作用:

  • 对于一个没有载入或者想动态载入的界面,都需要使用LayoutInflater.inflater(...)方法载入。
  • 对于一个已经载入的布局文件,可以使用Activity.findViewById(int id)方法获取界面的元素。

  LayoutInflater是一个抽象类,其声名如下:

1 public abstract class LayoutInflater extends Object{}

  可以通过以下三种方法,获取LayoutInflater实例对象:

1 // 调用Activity的getLayoutInflater() 
2 LayoutInflater inflater = getLayoutInflater();
3 LayoutInflater inflater = LayoutInflater.from(context);  
4 LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 

posted @ 2016-03-23 23:01  naray  阅读(263)  评论(0编辑  收藏  举报