LayoutInflater用法小结

       学习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); 

posted @ 2011-12-26 21:29  浮沉之主  阅读(343)  评论(0编辑  收藏  举报