LayoutInflater 总结

接触Android一段时间后,便对LayoutInflater有很多的疑问,因为它实在有太多表达方式了。各种表达方式适合在不同的地方使用,接下来,我们来总结一下。

1.它有什么作用?

我们经常使用setContentView(R.layout.xxx)载入我们需要的layout。从而用findViewById()找出里面的控件,但是如果我们需要载入其它layout呢。你可以简单地认为 LayoutInflater就是获得layout下的xml资源。

2.LayoutInflater的基本用法

第一种写法如下:

LayoutInflater layoutInflater = LayoutInflater.from(context);

第二种写法如下:

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

第三种写法如下:

LayoutInflater inflater=this.getLayoutInflater();

其实第一种就是第二种的简单写法,只是Android给我们做了一下封装而已。

第一种和第二种在一般情况下都可以用,但第三种必须在继承Activity的类中使用。

如:

public class MainActivity extends Activity {

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

LayoutInflater inflater=this.getLayoutInflater();

得到了LayoutInflater的实例之后就可以调用它的inflate()方法来加载布局了。

inflate(int resource, ViewGroup root, boolean attachToRoot)

解释一下这三个参数:

1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。

2. 如果root不为null,attachToRoot设为true,则会在加载的布局文件的最外层再嵌套一层root布局。

3. 如果root不为null,attachToRoot设为false,则root参数失去作用。

4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

这里有几种做法:

第一种:

LayoutInflater inflater=LayoutInflater.from(this);

RelativeLayout ff=(RelativeLayout)findViewById(R.id.ff);   //获得id=ff的RelativeLayout布局。

View item= inflater.inflate(R.layout.item, null);          //获得R.layout.item资源

ff.addView(item);                       //把它添加至RelativeLayout布局ff中。

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/ff"

android:layout_width="match_parent"

android:layout_height="match_parent" >

</RelativeLayout>

item.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="111"          />

</LinearLayout>

效果图:

第二种:

LayoutInflater inflater=LayoutInflater.from(this);

RelativeLayout ff=(RelativeLayout)findViewById(R.id.ff);   //获得id=ff的RelativeLayout布局。

View item= inflater.inflate(R.layout.item, ff,true);       //获得R.layout.item资源,把它添加至RelativeLayout布局ff中。

跟第一种对比后,可以发现inflate(int resource, ViewGroup root, boolean attachToRoot) 中的 ViewGroup root,实际上是调用了root.addView();

而此时,我们传入的是ff,即root=ff,root.addView()==>ff.addView(item);

如果第一种和第二种的效果是一样的。

我们从源代码上分析:

  if (root != null && attachToRoot) {
          root.addView(temp, params);
       } 

以上是LayoutInflater.class中的一段代码。从这段中就可以知道ViewGroup root, boolean attachToRoot这些参数有什么作用。

必须说的是ViewGroup root是“容器类”。也就是说传进去的值的类型一定要是 RelativeLayout、LinearLayout、 FrameLayout等“容器类”,像上面ff就是 RelativeLayout,所以才可以传进去。因为有些人分不清什么是ViewGroup,什么是View。    

第三种:
View item=View.inflate(Context context, int resource, ViewGroup root)

这一种比较简单,不用写LayoutInflater inflater=LayoutInflater.from(this) 之类的,就可以直接用了。ViewGroup root跟上面一样。

代码如:View item=View.inflate(this, R.layout.item, null);

LayoutInflater 总结

时间: 2024-08-08 14:17:22

LayoutInflater 总结的相关文章

Android数据填充器LayoutInflater

LayoutInflater类在应用程序中比较实用,可以叫布局填充器,也可以成为打气筒,意思就是将布局文件填充到自己想要的位置,LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化这个XML文件成为一个View,有点类似于类似于findViewById(),但是findViewById()是找xml布局文件下的具体widget控件(Button.TextView).对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate

三个案例带你看懂LayoutInflater中inflate方法两个参数和三个参数的区别

版权声明:本文为sang原创文章,转载请注明出处. 目录(?)[+] 关于inflate参数问题,我想很多人多多少少都了解一点,网上也有很多关于这方面介绍的文章,但是枯燥的理论或者翻译让很多小伙伴看完之后还是一脸懵逼,so,我今天想通过三个案例来让小伙伴彻底的搞清楚这个东东.本篇博客我们不讲源码,只看使用.源码的解读会在下一篇博文中带来. inflate方法从大范围来看,分两种,三个参数的构造方法和两个参数的构造方法.在这两类中又有细分,OK,那我们就把各种情况都来演示一遍. 1.三个参数的in

LayoutInflater inflate LayoutParams 简介

LayoutInflater简介 LayoutInflater就是布局填充器,作用是将xml布局文件转化为View对象. 可以通过以下两种方式获取LayoutInflater,其实他们是完全一样的 LayoutInflater layoutInflater = LayoutInflater.from(context); LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER

LayoutInflater

[LayoutInflater] 参考:http://blog.csdn.net/guolin_blog/article/details/12921889

LayoutInflater和inflate()

LayoutInflater LayoutInflater抽象类是用来加载XML布局文件(UI界面)的. 作用: 1.对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()或View.inflate()来载入,然后也可以通过findByIdView()获取界面的元素: 2.对于一个已经通过setContentView()载入的界面,就可以使用findViewById()方法来获得其中的界面元素. 获得 LayoutInflater 实例的三种方式 (

Android 中LayoutInflater原理分析

概述 在Android开发中LayoutInflater的应用非常普遍,可以将res/layout/下的xml布局文件,实例化为一个View或者ViewGroup的控件.与findViewById的作用类似,但是findViewById在xml布局文件中查找具体的控件,两者并不完全相同. 应用场景: 1.在一个没有载入或者想要动态载入的界面中,需要使用layoutInflater.inflate()来载入布局文件: 2.对于一个已经载入的界面,就可以使用findViewById方法来获得其中的界

android LayoutInflater、setContentView、findviewbyid 区分解析

一.LayoutInflater.inflate(layoutId, root, boolen)中三个参数的意义及作用 (这点可以参考鸿洋前辈博客地址:http://blog.csdn.net/lmj623565791/article/details/38171465) 主要知识点其实很少,如下: 若temp为layoutId所代表的布局,inflate的三种方法区分如下: View view=LayoutInflater.Inflate(layoutId, null )只创建temp ,返回t

Android - LayoutInflater

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

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

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

LayoutInflater(转)

在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用类似于findViewById(),不同的是LayoutInflater是用来获得布局文件对象的,而 findViewById()是用来获得具体控件的.LayoutInflater经常在BaseAdapter的getView方法中用到,用来获取整个View并返回.LayoutInflater的用法有