代码改变世界

[Android学习笔记]LayoutInflater的使用

2014-04-14 13:45  hellenism  阅读(136)  评论(0编辑  收藏  举报

LayoutInflater用于动态载入布局,然后获取到布局中定义完成的控件引用

常在动态加载布局,和Adapter中用到

 

使用步骤:
1.通过LayoutInflater加载xml布局文件
2.从载入的View中获取引用

 

ex:
不需要具体例子,需要用到的时候,自然会想起只有它能满足需求

View view = LayoutInflater.from(context).inflate(R.layout.textview_btn,null); // 动态加载textview_btn.xml布局
Button btn = (Button)view.findViewById(R.id.btn);                                     // 获取textview_btn.xml布局文件中定义的控件的引用
TextView textView = (TextView)view.findViewById(R.id.textView);                //
LayoutInflater