android的LayoutInflater用来得到一个布局文件,也就是xxx.xml,而我们常用的findviewbyid是用来取得布局文件里的控件或都布局。inflater即为填充的意思,也就是说得到一个布局文件来填充(把布局文件实例化成一个View然后返回该view)。使得方法如下: 三种办法可以得到:

1:在activity中的话可以直接调用getLayoutInflater()来获得。

2:通过服务获得:getSystemService. (Context.LAYOUT_INFLATER_SERVICE)。

3:通过ayoutInflater.from(Context context)方法来获得。 三种方法的代码如下:

 //方法3:
LayoutInflater lf=LayoutInflater.from(context);
View tempView=lf.inflate(R.layout.select_list, null);
//方法2:
LayoutInflater lf = (LayoutInflater)context.getSystemService.(Context.LAYOUT_INFLATER_SERVICE);
View tempView=lf.inflate(R.layout.select_list, null);
//方法1:
LayoutInflater lf = LayoutInflater.from(context);
View tempView=lf.inflate(R.layout.select_list, null);

在activity中的话上面的context可直接换成this。 得到返回的View后我们可以用他来给布局文件里的控件进行相应设置,如:

 TextView id=(TextView) tempView.findViewById(R.id.id);
TextView address=(TextView) tempView.findViewById(R.id.address); id.setText("123456");
address.setText("中国湖南长沙");

上面的获得View方法有两个参数,lf.inflate(int resource, ViewGroup root),第一个为整型的资源ID,也就是xml布局文件的id,后面为根View,如果为null则只创建和返回View,如果传入root,则把创建的View加为根View的子view。 LayoutInflater可以用来得到布局文件对里面的控件进行设置,也可以可以将得到的View返回给fragment,adapter等。 总之想怎么用就怎么用了...

android中LayoutInflater详解与使用的更多相关文章

  1. Android中Context详解 ---- 你所不知道的Context(转)

    Android中Context详解 ---- 你所不知道的Context(转)                                               本文出处 :http://b ...

  2. Android中Context详解 ---- 你所不知道的Context

    转自:http://blog.csdn.net/qinjuning/article/details/7310620Android中Context详解 ---- 你所不知道的Context 大家好,  ...

  3. android中getSystemService详解

        android的后台运行在很多service,它们在系统启动时被SystemServer开启,支持系统的正常工作,比如MountService监 听是否有SD卡安装及移除,ClipboardS ...

  4. Android中 ListView 详解(二)

    本文版权归 csdn noTice501 所有,转载请详细标明原作者及出处,以示尊重! 作者:noTice501 原文:http://blog.csdn.net/notice520/article/d ...

  5. Android中Handle详解

    上图为本人总结的Handler,网上发现一片总结很好的博客就copy过来:作为参考 Handler有何作用?如何使用? 一 .Handler作用和概念 包含线程队列和消息队列,实现异步的消息处理机制, ...

  6. Android中Context详解

    大家好,  今天给大家介绍下我们在应用开发中最熟悉而陌生的朋友-----Context类 ,说它熟悉,是应为我们在开发中时刻的在与它打交道,例如:Service.BroadcastReceiver.A ...

  7. 转:Android中Context详解 ---- 你所不知道的Context

    转:http://blog.csdn.net/qinjuning/article/details/7310620 转:http://blog.csdn.net/lmj623565791/article ...

  8. android中Actionbar详解

    1.什么是Action BarAction Bar被认为是新版Android系统中最重要的交互元素,在程序运行中一直置于顶部,主要起到的作用在于:1)突出显示一些重要操作(如“最新”.“搜索”等)2) ...

  9. Android中IntentService详解

    简单说,IntentService是继承于Service并处理异步请求的一个类,在IntentService内有一个工作线程来处理耗时操作,启动IntentService的方式和启动传统Service ...

随机推荐

  1. android的Broadcast receiver

    broadcast receiver是用来监听intent的. android大量使用了broadcast receiver,比如:开机.电话打进来.发送消息,手机电量过低 有两种方式注册broadc ...

  2. [GIF] Colors in GIF Loop Coder

    In this lesson we cover the different methods for defining and animating colors in GIF Loop Coder. f ...

  3. mydumper工作原理, seconds_behind_master的陷阱和pt-heartbeat (102)

    http://blog.itpub.net/15480802/cid-84815-list-1/

  4. Linux下安装ACE

      ACE 5.6下载地址:http://download.dre.vanderbilt.edu/ 方法一: 1. 解开ACE-install.sh文件    tar –zxvf ACE-5.6.ta ...

  5. 20个Linux系统监视工具

    需要监视Linux服务器的性能?试试这些内置的命令和一些附加的工具吧.大多数Linux发行版都集成了一些监视工具.这些工具可以获取有关系统活动的信息的详细指标.通过这些工具,你可以发现产生系统性能问题 ...

  6. Eval()和DataBinder Eval(Container DataItem,)的区别及用法

        ASP.NET 2.0改善了模板中的数据绑定操作把v1.x中的数据绑定语法DataBinder.Eval(Container.DataItem, fieldname)简化为Eval(fiel ...

  7. 介绍SmartUpload很好的网站

    附带链接:http://www.cnblogs.com/elleniou/archive/2012/09/24/2700583.html

  8. 替换SQL Server数据库中所有表的所有字段的某些内容

    declare @t varchar(255),@c varchar(255)  declare table_cursor cursor for select a.name,b.name  from  ...

  9. SQL 有父标识的 递归查询

    递归查询,临时表的高级应用 WITH temp AS ( --父项 SELECT * FROM Ar_Area WHERE Ar_Parent = UNION ALL --递归结果集中的下级 SELE ...

  10. Flie类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...