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 实例的三种方式

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

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

LayoutInflater inflater = LayoutInflater.from(context);   

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

getLayoutInflater():

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

1 public PhoneWindow(Context context) {
2
3         super(context);
4
5         mLayoutInflater = LayoutInflater.from(context);
6
7 }  

可以看出它其实是调用 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 对象,如下:

1 public View inflate (int resource, ViewGroup root)
2
3 public View inflate (XmlPullParser parser, ViewGroup root)
4
5 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
6
7 public View inflate (int resource, ViewGroup root, boolean attachToRoot)  

示意代码:

1 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
2 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
3
4 //EditText editText = (EditText)findViewById(R.id.content);// error
5
6 EditText editText = (EditText)view.findViewById(R.id.content);  

对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。

注意:

·inflate 方法与 findViewById 方法不同;

·inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;

·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。

时间: 2024-08-06 03:34:15

LayoutInflater的获取与使用的相关文章

Android获取LayoutInflater对象的方法总结

在写Android程序时,有时候会编写自定义的View,使用Inflater对象来将布局文件解析成一个View.本文主要目的是总结获取LayoutInflater对象的方法. 1.若能获取context对象,可以有以下几种方法: LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View child = inflater.inflate(R.la

2016.11.22经验积累(1.item长按与单机,2调用联系人.3.LayoutInflater获取方式)

很早之前就想建立一个微博,记录一下每天工作遇到的问题,第一是为了加深印象,第二是为了给遇见类似问题的同志提供思路.现在微博审批通过了,非常高兴. 1.listView长按事件返回值,之前一直以为是鸡肋,没啥用.昨天项目经理让加一个长按删除功能,结果长按同时触发单机进详情页的动作,断点跑了半天,最后百度,结果让我 吐了一口老血. 如何破:修改长按监听返回值.list长按返回值:false同时触发点击与双击,true只触发双击. 2.调用系统联系人,开始找了系统里封装的代码贴过来,结果测试提bug说

Android应用setContentView与LayoutInflater加载解析机制源码分析

[工匠若水 http://blog.csdn.net/yanbober 转载烦请注明出处,尊重分享成果] 1 背景 其实之所以要说这个话题有几个原因: 理解xml等控件是咋被显示的原理,通常大家写代码都是直接在onCreate里setContentView就完事,没怎么关注其实现原理. 前面分析<Android触摸屏事件派发机制详解与源码分析三(Activity篇)>时提到了一些关于布局嵌套的问题,当时没有深入解释. 所以接下来主要分析的就是View或者ViewGroup对象是如何添加至应用程

Android中LayoutInflater实例

LayoutInflater与findViewById的用法有很多可比较之处. 如:他们都可以通过id返回View. LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom_dialog,null); Button button = (Button) findViewById(R.id.button); 不同之处是:LayoutInflater是用来实例化整个布局文

android的ListView图文混搭

main4.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent

新闻client案例开发

新闻client.顾名思义就是看新闻用的client. 该新闻用到的知识模块有:android高级界面设计(Fragment.ViewPager),android网络通信(http通信).开源组件(xutils框架-HttpUtils模块.xutils框架-BitmapUtils模块),开源框架(library). 所需jar包:xUtils.gson.android-support-v4. 主界面滑动标签:library框架用于主界面标签 ?主界面ViewPager:ViewPager与上部分

在Android中获取LayoutInflater对象的方法

1.通过系统服务来获得,这是最标准的: LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 2.通过LayoutInflater的静态方法获得: LayoutInflater inflater = LayoutInflater.from(context); 3.如果在Activity中,可以直接使用Activity中的getLayoutInfla

Android-------ListView列表中获取EditText输入的值

最近项目的购物车中用列表中包含了留言功能, 需要获取EditText输入的内容,当购买多件商品时,就有点棘手了. 经过查资料解决了这个功能,并写了一个案例: 效果图: 可以在商品数据用一个字段来管理留言数据,这样就可以方便的操作了. 代码: public class MainActivity extends AppCompatActivity { ListView listView; @Override protected void onCreate(Bundle savedInstanceSt

android 获取相册列表的实现(三)

该项目实现的功能如下: 获取手机相册,点击每个相册之后进入该相册的图片列表界面,在图片列表界面可以实现图片多选,然后进入所选择的图片界面,在该界面内可以实现所选图片的上传等功能. 该项目最大特色: 1.获取相册列表,目前网络上面介绍获取相册的项目很少,本文专门讲述相册的获取. 2.使用Android-Universal-Image-Loader集成框架-第三方jar包加载本地图片,熟悉这个jar的开发者肯定不陌生,该jar包十分强大,除了可以获取网络图片,本地图片也是可以的.同时,通过引用第三方