`

LayoutInflater用法

 
阅读更多

在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用: 1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;

2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

LayoutInflater 是一个抽象类,在文档中如下声明:

publicabstractclass LayoutInflater extends Object 

 

获得 LayoutInflater 实例的三种方式

1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()

2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService

                                                (Context.LAYOUT_INFLATER_SERVICE);

3. LayoutInflater inflater = LayoutInflater.from(context);  

 

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

getLayoutInflater():

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

 

public PhoneWindow(Context context) {          super(context);          mLayoutInflater = LayoutInflater.from(context);  } 

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

 

LayoutInflater.from(context):

publicstatic LayoutInflater from(Context context) {       LayoutInflater LayoutInflater =               (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       if (LayoutInflater == null) {           thrownew AssertionError("LayoutInflater not found.");       }       return LayoutInflater;   }

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

 

结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。

 

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

分享到:
评论

相关推荐

    Android开发中LayoutInflater用法详解

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

    Android LayoutInflater的用法

    NULL 博文链接:https://yufeng-zhu.iteye.com/blog/1676207

    Android LayoutInflater深入分析及应用

    在Android中,如果是初级玩家,很可能对LayoutInflater不太熟悉,或许只是在Fragment的onCreateView()中模式化的使用过而已。但如果稍微有些工作经验的人就知道,这个类有多么重要,它是连接布局XMl和Java代码的桥梁...

    Android布局加载之LayoutInflater示例详解

    主要介绍了Android布局加载之LayoutInflater的相关资料,文中介绍的非常详细,对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。

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

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

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

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

    Aj_04的Android 中LayoutInflater的使用(源码)

    测试:Android 中LayoutInflater的使用 注意:Aj_04是用了调用另外一个界面,要注意调用的方法, 还一定还要在AndroidManifest.xml 中加上呢句:<activity android:name="LayoutInflaterDemo"></activity>

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

    主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下

    Android开发实现自定义Toast、LayoutInflater使用其他布局示例

    本文实例讲述了Android开发实现自定义Toast、LayoutInflater使用其他布局。分享给大家供大家参考,具体如下: 内容: 1.自定义样式toast 2.再活动中添加其他布局 实现效果: 步骤: 一、自定义View 引用zidingyixml...

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

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

    Android LayoutInflater中 Inflate()方法应用

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

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

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

    Android inflater 用法及不同点

    在 实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。...2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。 文档中的声明: public abs

    Android代码-QSkinLoader

    README分三部分:基本简介、使用方法、框架由来与架构设计。 如果不嫌麻烦,还可以去看文章夜间模式方案调研和QSkinLoader框架介绍 效果图 基本简介: QSkinLoader是一个支持多种场景的Android换肤框架。基本原理是...

Global site tag (gtag.js) - Google Analytics