部分内容来自: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中的控件。