`
lz1365871801
  • 浏览: 21600 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
社区版块
存档分类
最新评论

Android LayoutInflater详解

阅读更多

在实际开发中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_SERVICEWindowManager管理打开的窗口程序

LAYOUT_INFLATER_SERVICELayoutInflater取得xml里定义的view

ACTIVITY_SERVICEActivityManager管理应用程序的系统状态

POWER_SERVICEPowerManger电源的服务

ALARM_SERVICEAlarmManager闹钟的服务

NOTIFICATION_SERVICENotificationManager状态栏的服务

KEYGUARD_SERVICEKeyguardManager键盘锁的服务

LOCATION_SERVICELocationManager位置的服务,如GPS

SEARCH_SERVICESearchManager搜索的服务

VEBRATOR_SERVICEVebrator手机震动的服务

CONNECTIVITY_SERVICEConnectivity网络连接的服务

WIFI_SERVICEWifiManagerWi-Fi服务

TELEPHONY_SERVICETeleponyManager电话服务

 

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 等)。

 

转至:http://www.cnblogs.com/top5/archive/2012/05/04/2482328.html

分享到:
评论

相关推荐

    Android LayoutInflater加载布局详解及实例代码

    主要介绍了Android LayoutInflater加载布局详解及实例代码的相关资料,需要的朋友可以参考下

    Android LayoutInflater.inflate()详解及分析

    主要介绍了Android LayoutInflater.inflate()详解及分析的相关资料,需要的朋友可以参考下

    Android LayoutInflater.inflate源码分析

    主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下

    Android中使用LayoutInflater要注意的一些坑

    LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...

    Android布局加载之LayoutInflater示例详解

    前言 Activity 在界面创建时需要将 XML 布局文件中的内容加载进来,正如我们在 ListView 或者 RecyclerView 中需要将 Item 的布局加载进来一样,都是使用 ...由于 Android 系统源码中关于 Content 部分采用的是装饰模

    Android开发中LayoutInflater用法详解

    主要介绍了Android开发中LayoutInflater用法,结合实例形式分析了LayoutInflater类的功能、作用、使用方法及相关注意事项,需要的朋友可以参考下

    Android实现在列表List中显示半透明小窗体效果的控件用法详解

    本文实例讲述了Android实现在列表List中显示半透明小窗体效果的控件用法。分享给大家供大家参考,具体如下: Android 在列表List中显示半透明...import android.view.LayoutInflater; import android.view.View; import

    android之SeekBar控件用法详解

    MainActivity.java ... import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app....import android.view.LayoutInflater; import android.view.M

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    Android getViewById和getLayoutInflater().inflate()的详解及比较  由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以...

    Android组件popupwindow使用方法详解

    先看效果:  现在很多的应用效果都需要做的炫些,像UC,以及天天静听,效果很炫的,源码已经对外开放了,有兴趣的可以去研究下的  上源码 main.xml <?xml version=1.0 encoding=utf-8?... android:b

    Android简易电话拨号器实例详解

    安卓开发简易电话拨号器,具体内容如下 我是基于安卓4.2.2开发的,下面是我写的MainActivity.java代码: ... import android.support.v7.app.ActionBarActivity;...import android.view.LayoutInflater; impo

    详解Android PopupWindow怎么合理控制弹出位置(showAtLocation)

    说到PopupWindow,应该都会有种...View contentView = LayoutInflater.from(context).inflate(layoutId, null); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutP

    Android ListView里控件添加监听方法的实例详解

    Android ListView里控件添加监听方法的实例详解  关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现...

    Android 自定义ListView示例详解

    本文讲实现一个自定义列表的Android程序,程序将实现一个使用自定义的适配器(Adapter)绑定 数据,通过contextView.setTag绑定数据有按钮的ListView。 系统显示列表(ListView)时,首先会实例化一个适配器,本文...

Global site tag (gtag.js) - Google Analytics