android LayoutInflater.inflate()的参数及其用法
2016-04-05 11:32
501 查看
很多人在网上问LayoutInflater类的用法,以及inflate()方法参数的含义,现解释如下:
inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义
resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。
attachToRoot:是否将root附加到布局文件的根视图上
原文URL:http://blog.csdn.net/lovexieyuan520/article/details/9036673
=================================================================
以下是Android developer上的解释:
inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义
resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。
attachToRoot:是否将root附加到布局文件的根视图上
原文URL:http://blog.csdn.net/lovexieyuan520/article/details/9036673
=================================================================
以下是Android developer上的解释:
相关文章推荐
- android WindowManager可拖动悬浮按钮
- 深入浅析Android消息机制
- Android GridView属性集合
- Android Studio中获取SHA1或MD5的方法
- Android跨进程通信(IPC)——AIDL
- 利用drozer进行Android渗透测试 - AndroSecurity
- android基础部分再学习---再谈Service进程服务通信
- Android odex文件反编译
- [转]Android开发实践:Java层与Jni层的数组传递
- android textview 部分文字加颜色并可点击
- Android的ListView中添加数据库中的Image和RatingBar
- Android中Bitmap和Drawable
- Android自定义ListView实现下拉刷新,效果仿SwipeRefreshLayout
- android listview优化几种写法详细介绍
- Android Studio如何使用Git提交代码到GitHub和OsChina并解决冲突
- Android 带清除功能的输入框控件ClearEditText
- Android中viewpager轮播图小点儿
- 自己写的按钮点击倒计时控件
- 开启隐藏的开发者模式
- Android textview maxEms与maxLengt 属性