`
mickey_hou
  • 浏览: 237083 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

android LayoutInflater和inflate()方法的用法

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

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

(1).通过SystemService获得

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

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

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

    LayoutInflater inflater = LayoutInflater.from(context);

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

(3).

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

    View layout = 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) {

        LayoutInflater LayoutInflater = (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()方法应用

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

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

    最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 <LinearLayout android:id=@+id/ll_item_Group android:layout_width=match_parent android:layout_height=200dp android:background=...

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

    对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? 2.加载layout文件之后,又是怎么变成供...

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

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

    Android开发中LayoutInflater用法详解

    本文实例讲述了Android开发中LayoutInflater用法。分享给大家供大家参考,具体如下: 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/...

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

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

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

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

    Android inflater 用法及不同点

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

    谈谈Android里的Context的使用

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

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

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

    Android 安卓PopupWindow工具类

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

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

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

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

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

    高仿微信界面

    import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.chase.cn.money_of_my.R; /** * Created by Chase on 2017/2/6. */ public class Fragment_tab01 ...

    SwipeListViewTest项目

    <Button android:id="@+id/example_row_b_action_1" android:layout_width="0dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginRight="10dp" android:layout_weight="1" ...

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

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

    GoogleNavigationDrawerMenu-侧滑menu菜单.zip

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

    FlingCircleMenu:适用于Android的猛冲圈菜单

    FlingCircleMenu 适用于Android的猛冲圈菜单 截屏 用法 protected void onCreate(Bundle savedInstanceState)... View item1 = LayoutInflater.from(this).inflate(R.layout.menu_item_layout,flingCircleMenuLayout,f

Global site tag (gtag.js) - Google Analytics