Android之Inflate()

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

一、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)肯定会报错.

时间: 2024-08-02 18:54:47

Android之Inflate()的相关文章

Android LayoutInflater.inflate(int resource, ViewGroup root, boolean attachToRoot)的参数理解

方法inflate(int resource, ViewGroup root, boolean attachToRoot) 中 第一个参数传入布局的资源ID,生成fragment视图,第二个参数是视图的父视图,通常我们需要父视图来正确配置组件.第三个参数告知布局生成器是否将生成的视图添加给父视图. 我们新建一个项目测试一下: activity_main.xml <?xml version="1.0" encoding="utf-8"?> <Line

Android LayoutInflater.inflate的使用及源码分析

欢迎转载,转载请标明出处: http://blog.csdn.net/johnny901114/article/details/61913656 本文出自:[余志强的博客] 在实际开发中我们常常需要inflate要给布局然后添加到某个布局容器里面去, 要把xml布局文件转成一个View对象 需要使用LayoutInflater.inflate方法. 在开发中常常使用如下几种方式: inflater.inflate(layoutId, null); inflater.inflate(layoutI

android LayoutInflater.inflate详解

LayoutInflater概述 从XML文件中实例化一个布局成对应的View类, 它从来不会直接使用, 而是使用getLayoutInflater()或者getSystemService(String)来获得一个对应当前context的标准LayoutInflater 实例. 例如: 1 2 3 LayoutInflater inflater = (LayoutInflater)context.getSystemService       (Context.LAYOUT_INFLATER_SE

Android LayoutInflater.inflate使用上的问题解惑

最近在在使用LayoutInflater.inflate方法时遇到了一些问题,以前没有仔细看过此类的使用方法,故将其记录下来,方便日后查阅. 相信大家都知道LayoutInflater.inflate是在android开发遇到的一些使用频率是非常高的方法,如果使用不好的,就会出现一些奇怪的问题. 一个例子如下: 1,一个主布局文件如下 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

Android之Inflate()方法用途

转自:http://blog.csdn.net/andypan1314/article/details/6715928 Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能.最近做的一个项目就是这一点让我迷茫了好几天. android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别 区别在于: 如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layo

android LayoutInflater.inflate()的参数介绍

LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个布局文件: findViewById则是从布局文件中查找一个控件: 一.获取LayoutInflater对象有三种方法 LayoutInflater inflater=LayoutInflater.from(context); LayoutInflater inflater=getLayoutInf

Android之inflate用法:加载其他layout

通俗的说,inflate就相当于将一个xml中定义的布局找出来 如果你的Activity里用到别的layout,如显示图片的对话框,其layout布局文件为view.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientati

Android layoutInflate.inflate 方法详解,removeView()错误解决

错误: The specified child already has a parent. You must call removeView(). 解答: 这个错误很直白,就是你viewGroup.addView(childView); 中childView已经有父View了.错误原因很多,我主要讲下 mLayoutInflater.inflate(id, rootView, false);造成的这个错误.(该方法有两种,一种是2个参数,一种是3个参数). 2个参数: 第一个参数:layout的

Android LayoutInflater.inflate()的参数及其用法

很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下: inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件. 1.获取LayoutInflater对象有三种方法 LayoutInflater inflater=LayoutInflater.from(this); LayoutInflater inflat