在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater。LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的,而findViewById()是用来获得具体控件的。LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回。

LayoutInflater的用法有三种:

第一种方法:

LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);

第二种方法:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);

第三种方法:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);

方法一是方法三的简单写法,只是Android给我们做了一下封装而已。得到了LayoutInflater的实例之后就可以调用它的inflate()方法来加载布局了,如下所示:

layoutInflater.inflate(resourceId, root);

inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。

参考路径:http://blog.csdn.net/guolin_blog/article/details/12921889

下面是简单的使用示例:

public class LayoutInflaterActivity extends Activity {
private EditText et;
private Button btn; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 第一种方法
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
// 第二种方法
// LayoutInflater inflater = getLayoutInflater();
// View layout = inflater.inflate(R.layout.main, null);
// 第三种方法
// LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
// View layout = inflater.inflate(R.layout.main, null);
// 这里是通过事先获得的布局文件来实例化具体控件,并且可以根据情况自定义控件
et = (EditText) layout.findViewById(R.id.edittext);
et.setBackgroundColor(Color.YELLOW);
btn = (Button) layout.findViewById(R.id.btn);
btn.setBackgroundColor(Color.CYAN);
// 显示
setContentView(layout);
}
}

getSystemService是Activity中的方法,根据传入的name来取得对应的服务对象,这些服务名称参数都是Context类中的常量:

传入的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         电话服务

LayoutInflater的使用的更多相关文章

  1. 浅谈 LayoutInflater

    浅谈 LayoutInflater 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 源码:AndroidDemo/View 文中如有纰漏,欢迎大家留言指出. 在 Android 的 ...

  2. Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

    方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图 ...

  3. Android下LayoutInflater的使用

    在我们想XML布局文件转换为View对象的时候.我们都会使用LayoutInflate对象.顾名思义咋一眼就能看出来他是布局填充器.那么接下来看看LayoutInfalte的使用 总体分为 Layou ...

  4. Android 中 LayoutInflater 的使用

    一.LayoutInflater 的作用 我们一般使用 LayoutInflater 做一件事:View inflate(int resource, ViewGroup root); inflate( ...

  5. Android 生成LayoutInflater的三种方式

    通俗的说,inflate就相当于将一个xml中定义的布局找出来. 因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组 ...

  6. Android LayoutInflater详解

      在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且 ...

  7. [Android] 转-LayoutInflater丢失View的LayoutParams

    原文地址:http://lmbj.net/blog/layoutinflater-and-layoutparams/ View view = inflater.inflate(R.layout.ite ...

  8. 6、Android之LayoutInflater.inflate()

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  9. Android 查缺补漏之Adapter 和 LayoutInflater

    在之前的博客我也讲过 Adapter 和 inflater,但发现讲的不够清楚,好多读者看后有疑问,今天就结合API单独讲一次. Adapter : An Adapter object acts as ...

随机推荐

  1. node.js表单——formidable/////z

    node.js表单--formidable   node处理表单请求,需要用到formidable包.安装formidable包的命令如下: npm install formidable 安装pack ...

  2. php 截取代码方法(140个字后的。)

    //截取摘要public static function mbsubstr($str){    $strleng = mb_strlen($str,"utf8");    $mbs ...

  3. 【003:jsoncpp的简单使用】

    #include <json/json.h> #include <iostream> #include <string> using namespace std; ...

  4. URL请求工具

    工作中有个需求,定期请求多个URL.“定期”采用计划任务实现,请求URL,虽说start url可以实现,但不灵活.自己制作了个专门请求URL的工具,并记录请求结果. 控制台程序代码: class P ...

  5. js 获取根目录 获取参数

    function getRootPath() { //获取当前网址,如: http://localhost:8083/uimcardprj/share/meun.jsp var curWwwPath ...

  6. Asp.net在IE10、IE11下事件丢失经验总结

    asp.net4.0出生得比IE10早,所以asp.net4.0以前版本不认识IE10 的 User-Agent 标头,导致的后果就是ASP.NET 特定功能失效,例如:页面报错__doPostBac ...

  7. 不可错过的炒鸡棒的js迷你库

    小而美被实践是最好用的,这里收藏了一些很好用的js库,他们都功能单一且非常小. COOKIE.JS  https://github.com/js-coder/cookie.js 如果你操作过cooki ...

  8. C# 利用反射

    .NET基础篇——反射的奥妙 C#获取实体类属性名和值 | 遍历类对象 c#通过反射获取类上的自定义特性 C#利用反射+特性实现简单的实体映射数据库操作类

  9. Python GUI 背景色与语法高亮主题配置

    [补充] Python GUI 中 :ALT+P 可以重复上一条命令. ---------------------------------------------------------------- ...

  10. linux的whoami, who指令

    who 这个命令重点在用来查看当前有那些用户登录到了本台机器上 whoami 显示的是有效用户用户名 ,是当前操作用户的用户名 who am i == who -m 显示的是实际用户的用户名,即用户登 ...