Android--inflater 3种方式将xml中转为view

采用布局填充器把xml文件转换成view对象

3种方式将xml中转为view

一、3种方式将xml中转为view

方式一:LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

最原始的方式

// 方式一:原始方式:Context.getSystemService("服务名")
LayoutInflater inflater = (LayoutInflater) MainActivity. this .getSystemService(Context. LAYOUT_INFLATER_SERVICE );
// View list_item = inflater.inflate(R.id.list_item, null);//不是id中的list_item而是layout中的list_item
View list_item = inflater.inflate(R.layout. list_item , null ); // 参数2表示是否独立



方式二:LayoutInflater inflater2 = LayoutInflater.from(MainActivity.this);

底层是方式一

// 方式二:LayoutInflater.from(Context)
// LayoutInflater inflater2 = LayoutInflater.from(MainActivity.this);
// View list_item = inflater2.inflate(R.layout.list_item, null);

底层实现:
public static LayoutInflater from (Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context. LAYOUT_INFLATER_SERVICE );
if (LayoutInflater == null ) {
<span style="white-space:pre">	</span>throw new AssertionError( "LayoutInflater not found." );
}
return LayoutInflater;
}



方式三:View list_item = View.inflate(MainActivity.this, R.layout.list_item, null);

底层是方式二和方式一

public static View inflate (Context context, int resource, ViewGroup root) {
LayoutInflater factory = LayoutInflater.from(context);
return factory.inflate(resource, root);
} 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值