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

相关文章:

  • 2022-12-23
  • 2021-08-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-28
  • 2021-09-05
  • 2021-04-22
  • 2021-12-14
相关资源
相似解决方案