Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Android开发心得:LayoutInflater及inflate方法

引言:Android开发心得笔记,先看一段代码public void OnCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);这个是Activity创建时执行的方法,其中最后一句大家应该都见过,这个加载布局的最简单的方法,但是如果,你需要动态加载布局,那就需要使用其他办法。那就是LayoutInfater的inflate方法。一。获得 LayoutInflater 实例的三种方式1.LayoutInflater inflater = getLayoutInflater();  //调用Activity的getLayoutInflater()2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);3. LayoutInflater inflater = LayoutInflater.from(context);二。inflate方法public View inflate(int Resourece,ViewGroup root)作用:填充一个新的视图层次结构从指定的XML资源文件中reSource:View的layout的IDroot: 生成的层次结构的根视图return 填充的层次结构的根视图如果参数root提供了,那么root就是根视图;否则填充的XML文件的根就是根视图。其余几个重载的inflate函数类似。在View类中,也有inflate方法,相关的可以看下这篇文章 http://www.linuxidc.com/Linux/2014-11/108818.htm结束语:这段代码和之前说的那最后一句是一个意思:LayoutInflater inflate = LayoutInflater.from(this);
View view = inflate.inflate(R.layout.main,null);
setContentView(view);Android 4.4.4 发布下载 http://www.linuxidc.com/Linux/2014-06/103467.htm最简单的Ubuntu Touch & Android 双系统安装方式 http://www.linuxidc.com/Linux/2014-01/94881.htm在Nexus上实现Ubuntu和Android 4.4.2 双启动 http://www.linuxidc.com/Linux/2014-05/101849.htmUbuntu 14.04 配置 Android SDK 开发环境 http://www.linuxidc.com/Linux/2014-05/101039.htm64位Ubuntu 11.10下Android开发环境的搭建(JDK+Eclipse+ADT+Android SDK详细) http://www.linuxidc.com/Linux/2013-06/85303.htmUbuntu 14.04 x64配置Android 4.4 kitkat编译环境的方法 http://www.linuxidc.com/Linux/2014-04/101148.htmUbuntu 12.10 x64 安装 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-11/108817.htm