Inflate()---Android之Inflate()方法用途

来源:互联网 发布:苏州 青少年编程 编辑:程序博客网 时间:2024/05/16 06:53

Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。

android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别

区别在于:

如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layout上的其他组件的内容,你就必须用inflate()方法先将对话框的layout找出来,然后再用findViewById()找到它上面的其它组件。例如:

[html] view plaincopyprint?
  1. View view1=View.inflate(this,R.layout.dialog_layout,null);  
  2.     
  3.   TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
  4.     
  5.   dialogTV.setText("abcd");  
[html] view plain copy
 print?
  1. View view1=View.inflate(this,R.layout.dialog_layout,null);  
  2.     
  3.   TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);  
  4.     
  5.   dialogTV.setText("abcd");  
注:R.id.dialog_tv是在对话框layout上的组件,而这时若直接用this.findViewById(R.id.dialog_tv)肯定会报错。


[html] view plaincopyprint?
  1. View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  
[html] view plain copy
 print?
  1. View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();  

Inflate()或可理解为“隐性膨胀”,隐性摆放在view里,inflate()前只是获得控件,但没有大小没有在View里占据空间,inflate()后有一定大小,只是出于隐藏状态.

 

LayoutInflater和inflate的用法

在这里用Tabhost的例子来说明:

[java] view plaincopyprint?
  1. package cn.csdn.activity;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.TabHost;  
  7.   
  8. public class TabHostActivity extends TabActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.   
  13.         super.onCreate(savedInstanceState);  
  14.         TabHost tabhost = this.getTabHost();  
  15.   
  16.         /** 
  17.          * LayoutInflater这个类的作用类似于findViewById(), 
  18.          * 不同点: 
  19.          *     LayoutInflater是用来找layout下xml布局文件的,而且它会实例化 
  20.          *     findViewById()是找具体xml布局文件下的具体widget控件,比如:Button按钮 
  21.          *  
  22.          *  
  23.          *  
  24.          * inflate就相当于将一个xml中定义的布局找出来.     
  25.          * 因为如果在一个Activity文件里直接用findViewById()这个方法的话, 
  26.          * 那么它所对应的是setConentView()中调用的那个layout里的组件.    
  27.          * 因此如果在同样的Activity里用到别的layout的话, 
  28.          *     而且你还要设置这个layout里的组件(比如:ImageView,TextView)上的内容, 
  29.          *     那么你就必须用inflate()先将这个layout找出来, 然后再用这个layout对象去找到它上面的组件 
  30.          *     然后进行一系列的操作 
  31.          *      
  32.          *     inflate()方法中参数: 
  33.          *       1.想要用的布局文件的id 
  34.          *       2.持有选项卡的内容,获取FrameLayout 
  35.          *       3.true:将此处解析的xml文件做为根视图View 
  36.          */  
  37.         LayoutInflater.from(this).inflate(R.layout.tabhost_layout,  
  38.                 tabhost.getTabContentView(), true);  
  39.   
  40.           
  41.         /**在这里添加的时候: 
  42.          *       1.必须指定 tab 的内容,必须为 id, 即:setContent(R.id.text) 
  43.          *       2.必须设置tab 上的文字或图片  , 即:setIndicator("已接电话") 
  44.          *       3.返回一个 TabHost.TabSpec 对象,其参数用于标识一个 tab 的 tag,即:newTabSpec("tab1") 
  45.         */  
  46.         tabhost.addTab(tabhost.newTabSpec("tab1").setIndicator("已接电话")  
  47.                 .setContent(R.id.text));  
  48.           
  49.         tabhost.addTab(tabhost.newTabSpec("tab2").setIndicator("呼出电话",  
  50.                 getResources().getDrawable(R.drawable.ic_launcher))  
  51.                 .setContent(R.id.text));  
  52.           
  53.         tabhost.addTab(tabhost.newTabSpec("tab3").setIndicator("未接电话")  
  54.                 .setContent(R.id.text));  
  55.     }  
  56. }  

 

一、LayoutInflater

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

例:View toastRoot = getLayoutInflater().inflate(R.layout.toast, null);


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


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

例:View convertView = LayoutInflater.from(mContext).inflate(R.layout.activity_contact, null);


二、inflate

通俗的说,inflate就相当于将一个xml中定义的布局找出来.因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.


因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如:

  

  View view=View.inflate(this,R.layout.dialog_layout,null);

       TextView dialogTV=(TextView)view.findViewById(R.id.dialog_tv);

  

  dialogTV.setText("abcd");

  

  如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.

0 0
原创粉丝点击