Android LayoutInflater了解及使用
2016-10-13 17:45
501 查看
LayoutInflater(布局加载器)
布局加载器的作用简要来说就是 将xml文件定义的View转化成View对象实例有三种获得实例的方法:
LayoutInflater LayoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater layoutInflater = LayoutInflater.from(context);
LayoutInflater layoutInflater = getLayoutInflater();
注:第一种方法获取系统提供的布局加载器,第二种方法是第一个方法精简版,第三种适用于可视窗口的getLayoutInflater(),比如activity
获取到对象了,我们要怎么用呢:
inflate
inflate有多个重载方法public View inflate(int resource, boolean attachToRoot)
public View inflate(int resource, ViewGroup root, boolean attachToRoot)
public View inflate(XmlPullParser parser, ViewGroup root)
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
这些方法最终调用的还是最后一个方法,我们不管,我们拿来用就行。
参数
resource: 这就是要加载的xml文件。也就是 R.layout.*root: 包裹被载入的xml布局的根布局
attachToRoot: 决定参数2如何布局,这是个boolean类型。当它为true时,加载的xml将会在外层再裹上一层的root的布局。如果它为 false 时,加载的layout最外层的width和height将会和root的一样。也表示你写的width和height将会无效。
注! 当root 为null时,如下。加载的layout的width和height将会是默认的布局。
public View inflate(R.layout.XXX,null)
如需深入了解LayoutInflater,请移步LayoutInflater源码解析
相关文章推荐
- 【Android开发经验】LayoutInflater——80%的Android程序员对它并不了解甚至错误使用
- 【Android开发经验】LayoutInflater—— 你可能对它并不了解甚至错误使用
- 【Android开发经验】LayoutInflater——80%的Android程序员对它并不了解甚至错误使用
- 【Android开发经验】LayoutInflater—— 你可能对它并不了解甚至错误使用
- LayoutInflater——80%的Android程序员对它并不了解甚至错误使用
- 【Android开发经验】LayoutInflater——Android程序员对它并不了解甚至错误使用
- 【转】LayoutInflater——80%的Android程序员对它并不了解甚至错误使用
- Android中利用LayoutInflater使用Dialog
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android高手进阶教程(五)之 ----Android 中LayoutInflater的使用
- android中LayoutInflater的使用
- 【转】Android 中LayoutInflater的使用
- Android 中LayoutInflater的使用
- Android 中LayoutInflater的使用!
- android中LayoutInflater的使用
- android 入门学习笔记 LayoutInflater的使用
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- android中LayoutInflater的使用
- Android高手进阶教程(五)之----Android 中LayoutInflater的使用!
- Android中LayoutInflater的使用 更新中....