View.inflate 和 inflater.inflate 区别

1. LayoutInflater. from (RecylerActivity. this ) .inflate(R.layout. my_text_view , viewGroup ,false ) ;

2. View. inflate (RecylerActivity. this,  R.layout. my_text_view , null );

这两种 inflate 的方法有什么区别呢;

首先我们通过看源码得知View。inflate()方法实际上调用的也是LayoutInflater.from(int resource, ViewGroup root, boolean attachToRoo);

其中最主要的二者区别就在于 ViewGroup  root 这个参数没有传;
下面我们来看看inflate  方法的介绍


public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Added in API level 1
Inflate a new view hierarchy from the specified xml resource. Throws InflateException if there is an error.
Parameters
resource
ID for an XML layout resource to load (e.g., R.layout.main_page)
root
Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)
attachToRoot
Whether the inflated hierarchy should be attached to the root parameter? If false, root is only used to create the correct subclass of LayoutParams for the root view in the XML.
Returns
The root View of the inflated hierarchy. If root was supplied and attachToRoot is true, this is root; otherwise it is the root of the inflated XML file.


作用:填充一个新的视图层次结构从指定的XML资源文件中
reSource
ViewlayoutID
root
 生成的层次结构的根视图
return 
填充的层次结构的根视图。如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。

其余几个重载的inflate函数类似。



那么root因为第二传的是null所以他的parent 就是传进去的 xml resource;第一个传了root 所以他的parent就是ViewGroup;

所以就会导致启动的应用
1. 

2.


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值