inflate方法总共有四种形式,把xml表达的layout转化为view. This class is used to instantiate layout xml files into its corresponding view object. It is never be used directly——use getLayoutInflater() or getSystemService(String)getLayoutInflate() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up that is already hook up to the current context and correct configured for the device you are running on.
1. Context.public abstract object getSystemService(String name)
2. 两种获得LayoutInflater的方法
a. 通过SystemService获得
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLEATER_SERVICE);
b. 从给定的context中获取
Public static LayoutInflater from(Context context)
c. 两者的区别:实际上是一样的,源码
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
3. LayoutInflater.inflate()将Layout文件转换为View,专门供Layout使用的Inflater。虽然Layout也是View的子类,但在android中如果想将xml中的Layout转换为View放入.java代码中操作,只能通过Inflater,而不能通过findViewById()。
4. LinearLayout linearLayout =
(LinearLayout) findViewById(R.id.placeslist_linearlayout);
linearLayout.addView(place_type_text);
5. 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)
6.不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件(如:Button,TextView等)。
分享到:
相关推荐
LayoutInflater源码分析 inflate方法的区别
Js算法 zip_deflate(压缩)与zip_inflate(解压),
最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 android:layout_width=match_parent android:layout_height=200dp android:background=#FF0000 android:orientation=vertical> ...
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友
LayoutInflater.from(context).inflate()方法的调研
相信大家对LayoutInflate都不陌生,特别在ListView的Adapter的getView方法中基本都会出现,使用inflate方法去加载一个布局,用于ListView的每个Item的布局。Inflate有三个参数,我在初学Android的时候这么理解的:...
inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。 其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。 int resource,也就是...
LayoutInflater的inflate方法相信大家都不陌生,在Fragment的onCreateView中或者在BaseAdapter的getView方法中我们都会经常用这个方法来实例化出我们需要的View. 假设我们有一个需要实例化的布局文件menu_item.xml:...
Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google
LayoutInflater的inflate 方法的三个参数都代表什么意思? 系统对 merge、include 是如何处理的 merge 标签为什么可以起到优化布局的效果? XML 中的 View 是如何被实例化的? 为什么复杂布局会产生卡顿?在 Android...
前阶段的Android学习总结一、Android Studio导入现有工程的几个步骤二、这里推荐一篇好文三、四大应用组件之ContentProvider四、Git五、View...八、一些小的注意事项九、数据库学习中遇见的BUG十、inflate 方法...
通常,在编写页面时,每个人都通过xml编写布局,然后通过set contentView或LayoutInflater.from(context).inflate方法将xml布局加载到内存中。 优势 良好的可维护性 支持即时预览 清晰的代码结构 坏处 读取xml非常...
由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以看下。 LayoutInflater 要明白这个问题首先要知道什么是...
* 此时只会会执行Run()方法,但是不会开启新的线程 */ //Handler h = new Handler(); //h.post(r); /* * 在java中真正开启线程的方法是这样的。 */ Thread t = new Thread(r); t.start(); ...
已在XML中写入布局ID(android:layout="@layout/viewstub_layout1"),直接inflate填充。 方法二(for example:vs_view2): 没有在XML中写入布局ID,需要设置布局ID后(setLayoutResource),inflate填充。 ...
总是重写Recyclerview.Adapter的方法是不是很烦?一个基类帮你搞定一切,利用Google的DataBinding写出极其精简的代码