LayoutInflater

本文详细介绍了LayoutInflater的概念及作用,展示了三种获取LayoutInflater实例的方法,并解释了from方法的特性,即根据不同的Context对象返回相应的LayoutInflater实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、什么是LayoutInflater?

LayoutInflater是一个视图填充器;使用它来把布局转为一个View。

二、LayoutInflater的获取

第一种方式: 从给定的上下文中获取LayoutInflater:

LayoutInflater  inflater = LayoutInflater.from(context);

第二种方式:在Activity中获取LayoutInflater:

LayoutInflater inflater = getLayoutInflater();

第三种方式:通过context.getSystemService()获取:

LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

LayoutInflater的from方法

先给出结论:我们通过LayoutInflater的from(Context context)方法获取一个LayoutInflater对象。传递不同的Context对象,获取到的LayoutInflater对象也不同。每一个Activity 都会持有一个 LayoutInflater 对象, 如果每次传递的Context对象都是同一个Activity 对象,只会创建一个 LayoutInflater 对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值