`

layoutinflater详解

 
阅读更多

LayoutInflate说明

 

 这个类用来将XML的布局文件变成一个View的类。处于性能上的考虑,从XML的布局文件生成一个View类,

这个过程是在应用的构建阶段完成的,而不是在应用运行阶段进行的。


  1. LayoutInflater  inflate  =  (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  2. LinearLayout  layout  =  (LinearLayout) inflate.inflate(R.layout.ex01_07,null);  

findViewById是针对当前视图树的查找,而LayoutInflater是动态生成View,当你不能再当前视图树下找到View的时候,用LayoutInflater 

      LayoutInflater inflate = LayoutInflater.from(this) 从给定的context中获取LayoutInflater实例   


通俗的说, inflate() 就相当于将一个xml中定义的布局找出来.
因为在一个Activity里如果直接用findViewById()的话,对应的是setConentView()的那个layout里的组件.

因此如果你的Activity里如果用到别的layout,比如对话框上的layout,你还要设置对话框上的layout里的组件(像图片ImageView,文字TextView)上的内容,你就必须用inflate()先将对话框上的layout找出来,然后再用这个layout对象去找到它上面的组件,如: 

View view = View.inflate(this, R.layout.dialog_layout, null);
TextView dialogTV = (TextView) view.findViewById(R.id.dialog_tv);
dialogTV.setText("abcd");

如果组件R.id.dialog_tv是对话框上的组件,而你直接用this.findViewById(R.id.dialog_tv)肯定会报错.
View.inflate() 与 inflate.inflate(R.layout.ex01_07,null是一样的,而且要更简单一些.

分享到:
评论

相关推荐

    LayoutInflater详解

    LayoutInflater详解 里面有详细的操作哦

    Android布局加载之LayoutInflater示例详解

    主要介绍了Android布局加载之LayoutInflater的相关资料,文中介绍的非常详细,对大家具有一定的参考借鉴价值,需要的朋友们下面来一起看看吧。

    Android开发中LayoutInflater用法详解

    本文实例讲述了Android开发中LayoutInflater用法。分享给大家供大家参考,具体如下: 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/...

    Android LayoutInflater加载布局详解及实例代码

    Android LayoutInflater加载布局详解 对于有一定Android开发经验的同学来说,一定使用过LayoutInflater.inflater()来加载布局文件,但并不一定去深究过它的原理,比如 1.LayoutInflater为什么可以加载layout文件? ...

    Android LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate()详解 深入理解LayoutInflater.inflate() 由于我们很容易习惯公式化的预置代码,有时我们会忽略很优雅的细节。LayoutInflater以及它在Fragment的onCreateView()中填充View的方式...

    Android中使用LayoutInflater要注意的一些坑

    LayoutInflater类在我们日常开发中经常会用到,最近在使用中就遇到了一些问题,所有下面这篇文章主要给大家总结了关于Android中使用LayoutInflater要注意的一些坑,希望通过这篇能让大家避免走一些弯路,需要的朋友...

    Android LayoutInflater.inflate源码分析

    主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    Android getViewById和getLayoutInflater().inflate()的详解及比较  由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以...

    详解Android PopupWindow怎么合理控制弹出位置(showAtLocation)

    说到PopupWindow,应该都会有种...View contentView = LayoutInflater.from(context).inflate(layoutId, null); final PopupWindow popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutP

    Android ListView里控件添加监听方法的实例详解

    Android ListView里控件添加监听方法的实例详解  关于ListView,算是android中比较常见的控件,在ListView我们通常需要一个模板,这个模板指的不是住模块,而是配置显示在ListView里面的东西,今天做项目的时候发现...

    Android实现在列表List中显示半透明小窗体效果的控件用法详解

    本文实例讲述了Android实现在列表List中显示半透明小窗体效果的控件用法。分享给大家供大家参考,具体如下: Android 在列表List中显示半透明...import android.view.LayoutInflater; import android.view.View; import

    android之SeekBar控件用法详解

    MainActivity.java ... import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app....import android.view.LayoutInflater; import android.view.M

    Android简易电话拨号器实例详解

    安卓开发简易电话拨号器,具体内容如下 我是基于安卓4.2.2开发的,下面是我写的MainActivity.java代码: ... import android.support.v7.app.ActionBarActivity;...import android.view.LayoutInflater; impo

    Android中Fragment的加载方式与数据通信详解

    View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) inflater用于绑定Fragment的布局文件,同时将该布局转换成View对象并返回;container为Fragment的UI所在的父容器。...

Global site tag (gtag.js) - Google Analytics