LayoutInflate的使用

部分内容来自:http://www.cnblogs.com/DonkeyTomy/articles/2595606.html

在Android中,findViewById()函数的话是用于寻找xml布局文件下的具体控件。而如果我们想要载入另一个layout里的控件的话,就要使用inflate。

三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater)
this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

代码:
LayoutInflater mInflater =LayoutInflater.from(context);
View convertView = mInflater.inflate(R.layout.activity_speakers_home_item, null);
TextView text_name = (TextView)convertView.findViewById(R.id.speakers_name);

弄清楚每个语句的用意。
1.在第一个语句中,LayoutInflater类型是为了载入res\layout\中的名为context的xml文件。
2.第二个语句是将LayoutInflater进行实例化。可以理解为要将activity_speakers_home_item界面与context所在的界面连接起来,即指明是在context中引用activity_speakers_home_item中的控件。
3.第三个语句就是引用activity_speakers_home_item中的控件。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值