第一种:
public View inflate (int resource, ViewGroup root)
resource
:
View
的
layout
的
ID
root
:如果返回
null
,则将此
View
作为根
,
此时既可以应用此
View
中的其他控件了。
如果返回
!null,
则将默认的
layout
作为
View
的根。
第二种
:
public View inflate (XmlPullParser parser, ViewGroup root)
parser:
你需要解析
xml
的解析接口
root
:如果返回
null
,则将此
View
作为根
,
此时既可以应用此
View
中的其他控件了。
如果返回
!null,
则将默认的
layout
作为
View
的根。
第三种:
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
parser:
你需要解析
View
的
xml
的解析接口
root
:如果返回
null
,则将此
View
作为根
,
此时既可以应用此
View
中的其他控件了。
如果返回
!null,
则将默认的
layout
作为
View
的根。
attachToRoot
:
ture
: 也就将此解析的
xml
作为
View
根
false
:则为默认的
xml
,做为根视图
View
第四种:
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
resource
:
View
的
layout
的
ID
root
:如果返回
null
,则将此
View
作为根
,
此时既可以应用此
View
中的其他控件了。
如果返回
!null,
则将默认的
layout
作为
View
的根。
attachToRoot
:
ture
:也就将此解析的
xml
作为
View
根
false
:则为默认的
xml
,做为根视图
View
同时在此讲讲让我去
API
中去理解这四个函数的原因吧!
在
Activity
中:
大家是否知道
,
在
setContentView(new MySurfaceView(this))
后,此
Activity
中声明的
View
控件,
如:
TextView
为什么引用不到
layout
布局文件中的控件
ID
呢?初一看能够应用到,但是为什么编译就报空指针呢?
原因:在
setContentView(new MySurfaceView(this))
后,此时的
View
变为了根视图了,虽然能应用到
TextView
对应的
ID,
但是我在
MySurfaceView
中根本就没有这个对象,所以就报空指针!
解决办法:
View view = LayoutInflater.from(this).inflate(R.layout.passover, null);
注:每解析一次都会产生不同的对象,
然后你再引用没问题,使用自如了.
分享到:
相关推荐
LayoutInflater源码分析 inflate方法的区别
LayoutInflater.from(context).inflate()方法的调研
Android 中LayoutInflater.inflate()方法的介绍 最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。 android:layout_width=match_parent android:layout_height=200dp android:...
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
本文主要介绍Android 中Inflate 方法的用法, 在开发Android应用过程中,可以在程序中应用 Inflate()方法加载新布局,希望能帮助有需要的朋友
帮助你更好的理解LayoutInflate中的inflate方法,以后想怎么用怎么用
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
inflate这个方法总共有四种形式(见下面),目的都是把xml表述的layout转化为View对象。 其中有一个比较常用,View inflate(int resource, ViewGroup root),另三个,其实目的和这个差不多。 int resource,也就是...
LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...
3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...
在平时的开发过程中,我们经常会用LayoutInflater这个类,比如说在Fragment$onCreateView和RecyclerView.Adapter$onCreateViewHolder中都会用到。它的用法也无非就是LayoutInflater.inflate(resourceId, root, ...
本文实例讲述了Android开发中LayoutInflater用法。分享给大家供大家参考,具体如下: 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/...
由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以看下。 LayoutInflater 要明白这个问题首先要知道什么是...
return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_calendar_item, parent, false); } // @Override public void setDate(View itemVew, int year, int month, int day, boolean ...