使用LayoutInflater

来源:互联网 发布:软件工程硕士专业代码 编辑:程序博客网 时间:2024/05/16 19:18

一般来说,使用LayoutInflater把定义的xml转化为view,进而在程序中使用。这种方法可以有效的把UI和下层代码分隔。有几种方法可以实例化LayoutInflater

1.LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

 

2.直接在activity中调用getLayoutInflater()

 

3.context中有个静态函数public static LayoutInflater from(Context context)可以从context中得到LayoutInflater

 

另外还有个MenuInflater也是干这种事情的,只是转化菜单的xml文件到view而已

 MenuInflater inflater = getMenuInflater();