原文:http://daydayup1989.iteye.com/blog/824735

通俗的说,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),那么返回值将是null



三种方式可以生成LayoutInflater :
LayoutInflater inflater = LayoutInflater.from(this);
LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
然后调用inflate方法将xml布局文件转成View
public View inflate (int resource, ViewGroup root, boolean attachToRoot)

在View类中,也有inflate方法
public static View inflate (Context context, int resource, ViewGroup root)

更多相关文章

  1. Android布局管理(4)
  2. Android(安卓)Appcompat样式使用自定义Dialog
  3. android用户界面-组件Widget-选项卡Tab
  4. Android(安卓)Jetpack架构组件-Paging介绍及实践
  5. Android实现退出界面弹出提示对话框
  6. XUI 一个简洁而优雅的Android原生UI框架,解放你的双手!
  7. Android(安卓)Design Support 介绍
  8. 错误总结1,动态加载部分不能显示的原因
  9. Android之intents and intentFilters

随机推荐

  1. Android(安卓)Http请求失败解决方法
  2. Android中当前时间的动态显示
  3. android 小知识
  4. Android(安卓)Studio获取SHA1和MD5
  5. Android(安卓)权限中英文对照表
  6. Android上传图片到服务器并显示(后台用Ja
  7. Eclipse下如何导入第三方.class文件
  8. Android创建XMl文件 .
  9. Android(安卓)代码监控apk安装,卸载,替换
  10. Android的List Dialog实例