LayoutInflater用法小结

本文详细介绍了Android开发中LayoutInflater的使用方法,包括三种创建方式及如何通过它生成View对象,并提供了实例代码进行说明。

       学习BaseAdapter时遇到了LayoutInflater,百度了其用法~总结如下

       LayoutInflater的用法与findViewById()类似,通常用来将xml文件生成对应的View对象。

       摘抄一段:

LayoutInflater与findViewById( )的不同点:

LayoutInflater是将XML中的Layout转换为View放入.java代码中

findViewById()是找具体xml下的具体组件(如:Button,TextView,ImageView等)。

http://liangruijun.blog.51cto.com/3061169/750495)”

 

其用法有三种:

1.LayoutInflater inflater = LayoutInflater.from(this); 

   View layout = inflater.inflate(R.layout.main, null);

 

  • 2.LayoutInflater inflater = getLayoutInflater(); 
  •    View layout = inflater.inflate(R.layout.main, null);

3.LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 

   View layout = inflater.inflate(R.layout.main, null);

 

其后将layout作为一个View对象使用,其中控件的获取设置如下

TextView tx = (TextView)layout.findViewById(R.id.textview); 

转载于:https://www.cnblogs.com/shiyanch/archive/2011/12/26/2302557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值