LayoutInflater结合Context使用

1.Context的获取

一般我们用的是××Activity.this。其实还有另一种方式,从当前显示的某个view中获取,也就是调用view.getContext()方法。

2.LayoutInflater的获取

第一种方式:

LayoutInflater inflater = LayoutInflater.from(××Activity.this); from()方法api里是这样说的 Obtains the LayoutInflater from the given context.从给定的Context中获取LayoutInflater。

第二种方式:

调用Activity.getLayoutInflater()获得。我们来看API里是怎么说的: Convenience for calling Window.getLayoutInflater().继续点进去后有这样一句说明-- Quick access to the LayoutInflater instance that this Window retrieved from its Context.翻译一下,也就是说快速使用这个LayoutInflater实例,该实例是window从它的Context取到的。

第三种方式:

LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 这个也就是从系统服务中获得LayoutInflater。

3.LayoutInflater的用法:

View layout = inflater.inflate(R.layout.main, null); 注:系统加载xml,同样用的是LayoutInflater

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值