Android编程之LayoutInflater的inflate方法详解
2016-01-29 15:14
549 查看
LayoutInflater的inflate方法,在fragment的onCreateView方法中经常用到:
[java] view
plaincopy
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:
[java] view
plaincopy
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
[java] view
plaincopy
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
<
fdf8
li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:outside;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;background-color:rgb(248,248,248);">
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
所以,这里直接介绍里面调用这个方法即可:
[java] view
plaincopy
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:
[java] view
plaincopy
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
params = root.generateLayoutParams(attrs);
这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。
接着往下看:
[java] view
plaincopy
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
root.addView(temp, params);
如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。
如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。
[java] view
plaincopy
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater的inflate方法一共有四种,但我们日常用经常用到的就只有这两种:
[java] view
plaincopy
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}
[java] view
plaincopy
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
if (DEBUG) System.out.println("INFLATING from resource: " + resource);
XmlResourceParser parser = getContext().getResources().getLayout(resource);
<
fdf8
li style="border-style:none none none solid;border-left-width:3px;border-left-color:rgb(108,226,108);list-style:outside;line-height:18px;margin:0px !important;padding:0px 3px 0px 10px !important;background-color:rgb(248,248,248);">
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
所以,这里直接介绍里面调用这个方法即可:
[java] view
plaincopy
public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)
在这个方法里面,上半部分为xml解析的代码,这里就不贴出来,确实没什么东西可看。直接看中间部分的代码:
[java] view
plaincopy
ViewGroup.LayoutParams params = null;
if (root != null) {
if (DEBUG) {
System.out.println("Creating params from root: " +
root);
}
// Create layout params that match root, if supplied
params = root.generateLayoutParams(attrs);
if (!attachToRoot) {
// Set the layout params for temp if we are not
// attaching. (If we are, we use addView, below)
temp.setLayoutParams(params);
}
}
params = root.generateLayoutParams(attrs);
这段的意思是:如果调用inflate方法,传入了ViewGroup root参数,则会从root中得到由layout_width和layout_height组成的LayoutParams,在attachToRoot设置为false的话,就会对我们加载的视图View设置该LayoutParams。
接着往下看:
[java] view
plaincopy
// We are supposed to attach all the views we found (int temp)
// to root. Do that now.
if (root != null && attachToRoot) {
root.addView(temp, params);
}
// Decide whether to return the root that was passed in or the
// top view found in xml.
if (root == null || !attachToRoot) {
result = temp;
}
root.addView(temp, params);
如果设置了ViewGroup root参数,且attachToRoot设置为true的话,则将我们加载的视图做为子视图添加到root视图中。
如果我们ViewGroup root设置为空的话,就直接返回我们创建的视图;如果root不为空,且attachToRoot设置为false的话,就返回上面那段:对我们加载的视图View设置该LayoutParams。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories