原文:
Inflate()作用就是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。
android上还有一个与Inflate()类似功能的方法叫findViewById(),二者有时均可使用,但也有区别
区别在于:
如果你的Activity里用到别的layout,比如对话框layout,你还要设置这个layout上的其他组件的内容,你就必须用inflate()方法先将对话框的layout找出来,然后再用findViewById()找到它上面的其它组件。例如:
View view1=View.inflate(this,R.layout.dialog_layout,null);
TextViewdialogTV=(TextView)view1.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");
注:R.id.dialog_tv是在对话框layout上的组件,而这时若直接用this.findViewById(R.id.dialog_tv)肯定会报错。
View viewStub = ((ViewStub) findViewById(R.id.stubView)).inflate();
Inflate()或可理解为“隐性膨胀”,隐性摆放在view里,inflate()前只是获得控件,但没有大小没有在View里占据空间,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");
三种方式可以生成LayoutInflater:
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
publicViewinflate(intresource,ViewGrouproot,booleanattachToRoot)
在View类中,也有inflate方法
publicstaticViewinflate(Contextcontext,intresource,ViewGrouproot)
分享到:
相关推荐
本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友
Android 中LayoutInflater.inflate()方法的介绍 最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 <LinearLayout android:id=@+id/ll_item_Group android:layout_width=match_parent ...
这个demo主要实现了inflate的使用,希望可以帮助需要的同学.
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...
LayoutInflater源码分析 inflate方法的区别
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
Js算法 zip_deflate(压缩)与zip_inflate(解压),
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
LayoutInflater.from(context).inflate()方法的调研
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
本文实例讲述了Android开发中setContentView和inflate的区别。分享给大家供大家参考,具体如下: 一般用LayoutInflater做一件事:inflate inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化...
import android.os.Bundle; import android.os.Handler; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override protected void onCreate(Bundle ...
由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以看下。 LayoutInflater 要明白这个问题首先要知道什么是...
相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局。Inflate有三个参数,我在初学Android的时候这么理解的:...
matlab开发-Inflate。使用bls.gov的CPI通货膨胀计算器调整通货膨胀。
View contentView = inflater.inflate(R.layout.layout_pop, null); NewFloatMainWindow.getFloatMainWindow(MainActivity.this, NewFloatMainWindow.LOCATION_LEFT, contentView); 3.实现悬浮窗如此简单 readme-...