LayoutInflater

Inflater英文意思是膨胀,在Android中应该是扩展的意思吧。
LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout文件夹下的xml布局文件,并且实例化!而 findViewById()是找具体某一个xml下的具体 widget控件(如:Button,TextView等)。

获取它的用法有3种:

方法1:

LayoutInflater的静态函数:from(Context context) 获取:

static LayoutInflater from(Context context);

如:

Java代码 复制代码 收藏代码
  1. LayoutInflater inflater = LayoutInflater.from(this);
  2. View view=inflater.inflate(R.layout.ID, null);
  3. //或写成:
  4. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);
Java代码 复制代码 收藏代码
  1. LayoutInflater inflater = LayoutInflater.from(this);       
  2.   
  3. View view=inflater.inflate(R.layout.ID, null);        
  4.   
  5. //或写成:        
  6.   
  7. View view=LayoutInflater.from(this).inflate(R.layout.ID, null);     
LayoutInflater inflater = LayoutInflater.from(this);    

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

//或写成:     

View view=LayoutInflater.from(this).inflate(R.layout.ID, null);   

方法2:

由服务获取:

Java代码 复制代码 收藏代码
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService
  2. (Context.LAYOUT_INFLATER_SERVICE);
Java代码 复制代码 收藏代码
  1. LayoutInflater inflater = (LayoutInflater)context.getSystemService        
  2.  (Context.LAYOUT_INFLATER_SERVICE);   
  3.    
LayoutInflater inflater = (LayoutInflater)context.getSystemService     
 (Context.LAYOUT_INFLATER_SERVICE);
 

方法3:

调用Activity的getLayoutInflater() 函数获取LayoutInflater 对象。

 

setContentView和inflate区别

转:http://blog.163.com/promise_wg/blog/static/18912001420116241062211/

一般用LayoutInflater做一件事:inflate

inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象
其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。
int resource,也就是resource/layout文件在R文件中对应的ID,这个必须指定。
而ViewGroup root则可以是null,null时就只创建一个resource对应的View,不是null时,会将创建的view自动加为root的child。

setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的对象,有需要时再用setContentView(view)显示出来
一般在activity中通过setContentView()将界面显示出来,但是如果在非activity中如何对控件布局设置操作了,这需LayoutInflater动态加载
< TextView
android:id="@+id/tview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ATAAW.COM"
/>
< Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/button"
android:text="按钮"
/>
在程序中动态加载以上布局。
LayoutInflater flater = LayoutInflater.from(this);
View view = flater.inflate(R.layout.example, null);
获取布局中的控件。
button = (Button) view.findViewById(R.id.button);//这里的view为上面获取的view对象
textView = (TextView)view.findViewById(R.id.tview);

LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。

findViewById有两种形式
R.layout.xx是引用res/layout/xx.xml的布局文件(inflate 方法),R.id.xx是引用布局文件里面的组件,组件的id是xx(findViewById方法)。所有的组件id都能用R.id.xx来查看,但是组件不在setContentView()里面的layout中就无法使用,Activity.findViewById()会出现空指针异常
a. activity中的findViewById(int id)
b. View 中的findViewById(int id)
不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。

 

关于inflate的第3个参数

2012-2-19 09:24| 发布者: benben| 查看: 5168| 评论: 0

 

摘要: 方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前连个参数都好理解,我比较费解的是第3个参数。文档中的解释是:Whether the inflated hierarchy should be attached to the root paramete ...

 

 

方法 inflate(int resource, ViewGroup root, boolean attachToRoot) 中,前连个参数都好理解,我比较费解的是第3个参数。

文档中的解释是:Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
 
举个例子看一下
新建一个工程
 
工程包含两个xml文件
layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
   
    <FrameLayout
        android:id="@+id/ffff"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">FrameLayout>
 
LinearLayout>
 
layout/ffff.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"
    android:orientation="vertical" >
 
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox" />
 
LinearLayout>
 
接下来看activity中怎么写的
这里分3中情况
first, no attachToRoot params
activity 中的部分代码,注意看红色部分
        setContentView(R.layout.main);
        ViewGroup v = (ViewGroupfindViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffffv);
 

 
布局结构图

 
Second, params attachToRoot is false
View vv = LayoutInflater.from(this).inflate(R.layout.ffff, v, false);
 

发现没有了ffff.xml 中的内容
通过结构图查看,确实没有了

 
Third,
        ViewGroup v = (ViewGroupfindViewById(R.id.ffff);
        View vv = LayoutInflater.from(this).inflate(R.layout.ffffvfalse);
        v.addView(vv);
 
运行结果

 
呵呵,又有了。
 
所以这个参数的作用就是,是否把选取的视图加入到root中。false 的意思就是不添加到root中。可能需要我们手动添加。
原文地址:https://www.cnblogs.com/wangs/p/3327823.html