`

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 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):

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

 

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) 

 1:

  public View inflate (int resource, ViewGroup root)
  reSource:View的layout的ID
  root:如果为null,则将此View作为根,此时既可以应用此View中的其他控件了。
          如果!null,  则将默认的layout作为View的根。

 2:

  public View inflate ( XmlPullParser parser, ViewGroup root)
   parser:你需要解析xml的解析接口
   root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
          如果!null, 则将默认的layout作为View的根。

 3:

  public View inflate ( XmlPullParser parser, ViewGroup root, boolean attachToRoot)
   parser:你需要解析View的xml的解析接口
   root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。
          如果!null, 则将默认的layout作为View的根。
   attachToRoot:
   ture:也就将此解析的xml作为View根
   fase:则为默认的xml,做为根视图View

 4:

  public View inflate (int resource, ViewGroup root, boolean attachToRoot)

  resource:View的layout的ID

  root:如果null,则将此View作为根,此时既可以应用此View中的其他控件了。

           如果!null, 则将默认的layout作为View的根。

  attachToRoot:

  ture:也就将此解析的xml作为View根
  fase:则为默认的xml,做为根视图View

 

示意代码:

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); 

 同时在此讲讲让我去API中去理解这四个函数的原因吧!嘿嘿!你肯定又会多学一招!
在Activity中:
大家是否知道,在setContentView(new MySurfaceView(this))后,此Activity中声明的View控件,
如:TextView 为什么引用不到layout布局文件中的控件ID呢!初一看能够应用到,但是为什么编译就报空指针呢!原因:在setContentView(new MySurfaceView(this))后,此时的View变为了根视图了,虽然能应用到TextView对应的ID,但是我在 MySurfaceView中根本就没有这个对象,所以就报空指针咯!解决办法:
View view = LayoutInflater.from(this).inflate(R.layout.passover, null);注:每解析一次都会产生不同的对象
然后你再引用没问题,使用自如了。

分享到:
评论
1 楼 xuanyuanxiaoxue 2014-03-25  

相关推荐

    down-test-Android 获得 LayoutInflater 实例的三种方式

    down-test-Android 获得 LayoutInflater 实例的三种方式

    android-query

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(getContainerView(), container, false); aq = new AQuery(getActivity(),...

    Android LayoutInflater的用法

    在Android开发中,`LayoutInflater`是一个非常重要的工具类,它主要用于将XML布局文件转换为视图对象,使得我们可以动态地将界面元素添加到应用程序中。`LayoutInflater`是Android框架的一部分,它极大地增强了UI...

    Android 中LayoutInflater的使用

    在Android开发中,LayoutInflater是一个非常关键的工具,它主要用于将XML布局文件转换为视图对象。这个过程称为“实例化”或“.inflate”。通过LayoutInflater,我们可以动态地在运行时加载和插入用户界面元素,这...

    android-support-v7-recyclerview.jar

    View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_layout, parent, false); return new MyViewHolder(itemView); } @Override public void onBindViewHolder(MyViewHolder ...

    android oschina-android-app源码.rar

    同时,对于动态布局,可能涉及到LayoutInflater和DataBinding。 6. **权限管理** 在Android 6.0及以上版本,需要处理运行时权限。源码可能会包含如何使用` ActivityCompat`和`PermissionCompat`进行权限请求和管理...

    Android 中LayoutInflater(布局加载器)之实战篇Demo

    Android 中LayoutInflater(布局加载器)之实战篇 博客的Demo 博客地址: http://blog.csdn.net/l540675759/article/details/78112989 两种方式实现小红书的引导页: (1)自定义View (2)自定义LayoutInflater....

    Android 中级应用 一 LayoutInflater 的使用

    在Android开发中,`LayoutInflater`是一个至关重要的工具类,它主要负责将XML布局文件转换成View对象并添加到视图层次结构中。`LayoutInflater`的名字来源于"Layout Inflater",正如描述中提到的,它是对当前...

    Android--开发--PopupWindow下拉列表.rar

    - 使用LayoutInflater加载布局,并将其设置为PopupWindow的内容视图。 - 设置PopupWindow的宽度和高度,一般宽度设为MATCH_PARENT,高度可以是WRAP_CONTENT或根据需要设定的具体值。 - 设置PopupWindow是否能获得...

    android换肤包lib

    在Android应用开发中,换肤功能是一个非常受欢迎的特性,它允许用户根据个人喜好更改应用程序的界面风格。"android-skin-loader"是一个专门为Android平台设计的插件化换肤库,方便开发者集成到自己的项目中,为用户...

    android中LayoutInflater的使用.pdf

    在Android开发中,`LayoutInflater` 是一个至关重要的工具,它负责将XML布局文件转换为视图对象(View objects)。这个过程被称为布局的“实例化”或“膨胀”。`LayoutInflater` 提供了一种灵活的方式来动态地加载和...

    Android应用源码之android-styled-dialogs 可自定义样式的dialog-IT计算机-毕业设计.zip

    通过`LayoutInflater`将布局加载到Dialog中。 3. **DialogFragment**:为了保持良好的架构和避免内存泄漏,Android推荐使用`DialogFragment`来管理Dialog。`DialogFragment`继承自`Fragment`,可以独立于Activity...

    Android LayoutInflater深入分析及应用

    arser.START_TAG && type != XmlPullParser.END_DOCUMENT) { // Do nothing } if (type != XmlPullParser.START_TAG) { throw new InflateException(parser.getPositionDescription() + ": No start tag found!...

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

    Android LayoutInflater 是一个核心组件,它负责将XML布局文件转换为屏幕上的可交互视图对象。在Android应用开发中,我们通常使用LayoutInflater来动态地加载和插入布局,这在创建自定义视图、处理动态数据或者在...

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

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

    android-patterns-master.zip

    11. **建造者模式**:用于构建复杂对象,例如,使用Android的LayoutInflater构建View层次结构。 通过"android-patterns-master.zip"中的示例,你可以深入理解这些设计模式在Android开发中的实际应用,并能将它们...

    NFCampus-Android-Client-源码.rar

    `LayoutInflater`用于动态加载布局,提供更灵活的界面设计。此外,`LiveData`和`ViewModel`(来自Android Architecture Components)可能用于实现数据的实时更新,使得界面能在数据变化时自动更新,提高用户体验。 ...

    android-swipelistview Demo 例子使用

    在Android开发中,`SwipeListView`是一个非常实用的视图组件,它提供了滑动操作来显示额外的内容或者执行相应的动作,比如在列表项上滑动显示删除按钮。本示例将详细讲解如何在项目中使用`android-swipelistview`库...

    Android LayoutInflater.Factory主题切换

    首先,我们需要理解`LayoutInflater`在Android中的角色。`LayoutInflater`是负责将XML布局文件转换为视图对象(View)的关键类。它从资源文件中读取布局描述,并根据描述创建对应的View实例。通过`LayoutInflater`,...

Global site tag (gtag.js) - Google Analytics