对Android LayoutInflater的理解

来源:互联网 发布:2015中小企业数据统计 编辑:程序博客网 时间:2024/05/22 05:31
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是 LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具 体widget控件(如Button、TextView等)。
具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。

//在程序中动态加载以上布局。LayoutInflater flater =<span style="color:#FF0000;"> LayoutInflater.from(this)</span>;View view = flater<span style="color:#FF0000;">.inflate</span>(R.layout.example, null);//获取布局中的控件。button = (Button) <span style="color:#FF0000;">view.</span>findViewById(R.id.button);//这里的view为上面获取的view对象textView = (TextView)view.findViewById(R.id.tview);


0 0
原创粉丝点击