`
阿尔萨斯
  • 浏览: 4169271 次
社区版块
存档分类
最新评论

LayoutInflater和inflate()方法的用法

 
阅读更多

LayoutInflater作用是将layout的xml布局文件实例化为View类对象。

实现LayoutInflater的实例化共有3种方法,

(1).通过SystemService获得

LayoutInflaterinflater = (LayoutInflater)context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);

Viewview = inflater.inflate(R.layout.main, null);

(2).从给定的context中获得

LayoutInflaterinflater = LayoutInflater.from(context);

Viewview = inflater.inflate(R.layout.mian, null);

(3).

LayoutInflaterinflater =getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)

Viewlayout = inflater.inflate(R.layout.main, null);


其实,这三种方式本质是相同的,从源码中可以看出:

getLayoutInflater():

Activity的getLayoutInflater()方法是调用PhoneWindow的getLayoutInflater()方法,看一下该源代码:

publicPhoneWindow(Contextcontext) {

super(context);

mLayoutInflater= LayoutInflater.from(context);

}

可以看出它其实是调用LayoutInflater.from(context)。

LayoutInflater.from(context):

public static LayoutInflaterfrom(Context context) {

LayoutInflaterLayoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

if(LayoutInflater== null){

thrownew AssertionError("LayoutInflaternot found.");

}

returnLayoutInflater;

}

可以看出它其实调用context.getSystemService()。

public View inflate(int Resourece,ViewGrouproot)
作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource:View的layout的ID
root: 生成的层次结构的根视图
return 填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。

分享到:
评论

相关推荐

    Android 中LayoutInflater.inflate()方法的介绍

    最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 android:layout_width=match_parent android:layout_height=200dp android:background=#FF0000 android:orientation=vertical> ...

    Android LayoutInflater中 Inflate()方法应用

    本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友

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

    3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...

    Android开发中LayoutInflater用法详解

    本文实例讲述了Android开发中LayoutInflater用法。...1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入; 2、对于一个已经载入的界面,就可以使用Activiyt

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

    它的用法也无非就是LayoutInflater.inflate(resourceId, root, attachToRoot),第一个参数没什么好说的,但第二个和第三个参数结合起来会带来一定的迷惑性。之前有时候会发现界面布局上出了一些问题,查了很久之后...

    Android中LayoutInflater.inflater()的正确打开方式

    LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...

    Android inflater 用法及不同点

    1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入; 2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。 文档中的声明: public ...

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

     由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以看下。 LayoutInflater 要明白这个问题首先要知道什么是...

    安卓单任务多线程任意断点下载【源码】.rar

    安卓Android单任务多线程任意断点下载【源码】,包括了一个可复用的文件下载服务类用于...使用inflate(int resource, ViewGroup root)方法生成新的View  4.调用当前页面中某个容器的addView,将新创建的View添加进来

    SwipeMenuViewHolder:一个可以滑出条目操作菜单的RecycleView.ViewHolder

    用法:添加依赖compile 'cn.easydone.swipemenuviewholder:SwipeMenuViewHolder:0.1'在 Adapter 里继承 SwipeMenuViewHolder ,在 onCreateViewHolder 方法里 inflate 要拖动的布局和拖出来的布局。View ...

    PageAndPullToRefresh-分页加载和下拉刷新,几行代码完全搞定.zip

    介绍:分页加载和下拉刷新,几行代码完全搞定项目地址:https://github.com/tubeber/PageAndPullToRefresh运行效果:使用说明:本项目封装了分页请求和下拉刷新的全部过程,开发者只需要关注ListView子项...使用方法同2

    Android 安卓PopupWindow工具类

    文章目录引入效果图示例使用方法相关解释工具类封装最后奉上汪姑娘的Blog 引入 Android在需要类似于这样的弹窗会用到PopupWindow,所以,我为此封装了PopupWindow工具类,请往下看!!! 效果图 示例使用方法 ...

    X2C:增加版图加载速度200%

    通常,在编写页面时,每个人都通过xml编写布局,然后通过set contentView或LayoutInflater.from(context).inflate方法将xml布局加载到内存中。 优势 良好的可维护性 支持即时预览 清晰的代码结构 坏处 读取xml非常...

    Android使用setCustomTitle()方法自定义对话框标题

    Android有自带的对话框标题,但是不太美观,如果要给弹出的对话框设置一个自定义的标题,使用AlertDialog.Builder的setCustomTitle()方法。 运行效果如下,左边是点击第一个按钮,弹出Android系统自带的对话框(直接...

    自定义Dialog弹窗

    使用方法都有。public class BaseDialog extends Dialog { private View mContentView; public Context mContext; public LayoutInflater mInflater; public BaseDialog(Context context) { this(context, R....

    GoogleNavigationDrawerMenu-侧滑menu菜单.zip

    项目地址:https://github.com/Arasthel/GoogleNavigationDrawerMenu效果图:如何使用方法1. 直接用java代码创建 1. 首先你需要创建个内容页的布局文件<?xml version="1.0" encoding="utf-8"?>  xmlns:...

    MZBannerView-仿魅族BannerView,图片轮播控件,支持多种模式切换:普通ViewPager使用,普通Banner使用,仿魅族Banner使用。.zip

    使用很方便,具体使用方法和API 请看后面的示例。 --- 左图为魅族APP上的Banner效果,右图是高仿效果。MZBannerView 有以下功能:1 . 仿魅族BannerView 效果。2 . 当普通Banner 使用3 . 当普通ViewPager 使用。...

    谈谈Android里的Context的使用

    大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...

Global site tag (gtag.js) - Google Analytics