您的位置:首页 > 移动开发 > Android开发

inflate()方法详解和源码分析

2016-08-01 23:23 351 查看
在开发中,我们经常需要使用到LayoutInflater,通过该对象的inflate()方法,将一个layout布局文件实例化为View对象。

关于LayoutInflater对象的获取,参考博文:
http://blog.csdn.net/ruancoder/article/details/51760942

今天主要对inflate()方法的使用和源码进行分析。

(1).inflate()方法的使用
在实际使用中,我们一般会用到inflate的以下两个重载方法。

方法一:
public View inflate(int resource, ViewGroup root) {}


方法二:
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {}


其中方法一最为常见。

常见使用案例一:
View myView = LayoutInflater.from(context).inflate(R.layout.my_view, null);
将布局文件/res/layout/my_view.xml实例化为myView对象。

常见使用案例二:
ViewGroup viewRoot;
LayoutInflater.from(context).inflate(R.layout.my_view, viewRoot);
将布局文件/res/layout/my_view.xml实例化的View对象添加到viewRoot布局中。

那么方法一与方法二有什么区别呢?

进入方法一的源码,我们会发现内部调用的其实就是方法二,只是将方法二的第3个参数设为“root != null”。
public View inflate(int resource, ViewGroup root) {
return inflate(resource, root, root != null);
}


方法二中的参数和返回值释义:

参数:

resource:需要实例化的布局资源id。

root:ViewGroup类型视图组对象

attachToRoot:是否将resource实例化后的View添加到参数root中。

返回值:

如果root为空,直接返回resource实例化后的View对象;

如果root不为空,attachToRoot为true,将resource实例化为view对象,忽略view的最外层视图在xml布局中定义的属性,将view添加到root中,并将root返回。

如果root不为空,attachToRoot为false,将resource实例化为view对象,为view的最外层视图设置其在xml布局中定义的属性,并将view对象返回。

(2).inflate()方法的源码分析

进入inflate()方法内部。
public View inflate(int resource, ViewGroup root, boolean attachToRoot) {
final Resources res = getContext().getResources();
// 根据layout resource id,获取该布局的XmlResourceParser对象
final XmlResourceParser parser = res.getLayout(resource);
try {
return inflate(parser, root, attachToRoot);
} finally {
parser.close();
}
}
从方法内部可以看到,LayoutInflater使用的是pull解析器来解析xml布局文件的。获取到布局resource的XmlResourceParser对象后,接着进入下一个方法。

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {

// ...

// attrs是传入的布局layout在xml文件里面设置的属性集合
final AttributeSet attrs = Xml.asAttributeSet(parser);

// 将ViewGroup类型的参数root赋值给result
View result = root;

// temp是传入的参数resource的根布局View
final View temp = createViewFromTag(root, name, inflaterContext, attrs);

ViewGroup.LayoutParams params = null;

// 实例化temp视图内的所有子视图
rInflateChildren(parser, temp, attrs, true);

if (root != null) {
// 根据attrs属性集,创建布局参数params
params = root.generateLayoutParams(attrs);
// 如果temp不需要添加到root中,那么为temp设置布局参数params
if (!attachToRoot) {
temp.setLayoutParams(params);
}
}

if (root != null && attachToRoot) {
// 将temp添加到root中,并使用布局参数params
root.addView(temp, params);
}

if (root == null || !attachToRoot) {
// 将temp赋值给result(在此之前,result==root)
result = temp;
}

// ...

return result;
}
该方法是inflate()的关键,这里只抽取了需要关注的核心代码。

最开始,定义方法的返回值result,将参数root赋值给result,实例化参数resource赋值给temp。

接下来进入判断条件。

当root!=null时,根据resource的最外层view在xml中定义的的属性,创建布局参数params。如果!attachToRoot,为temp设置布局参数params。

当root!=null&&attachToRoot,将temp添加到root中,并使用上面创建的布局参数params。

当root==null||!attachToRoot,将temp赋值给result。

最后,将result返回。

逻辑看起来较繁琐,简单点可以这么理解。

只要root==null,无视attachToRoot的值,创建temp对象,返回temp。

当root!=null时,分两种情况。一,attachToRoot==true,将temp添加到root中,并使用布局参数params,返回root。二,attachToRoot==false,为temp设置布局参数params,返回temp。

(3).inflate()方法代码示例

在上面对inflate(int resource, ViewGroup root, boolean attachToRoot)方法的源码分析中,关于参数和返回值及其中的逻辑不难理解,主要是方法内是否为temp设置布局参数有些难以理解。下面使用代码示例进行说明。

创建MainActivity,并设置界面布局activity_main.xml。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"/>


新建一个textview.xml。这里故意给TextView的layout_width和layout_height设定了固定数值,并添加layout_gravity属性。方便后面看出差异。
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="230dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal"
android:background="#999999"
android:text="blog.csdn.net/ruancoder"
android:textColor="#ffffff"
android:textSize="18sp"/>


在MainActivity的onCreate()方法中,添加如下代码。
<pre name="code" class="java">package net.csdn.blog.ruancoder;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ViewGroup root = (ViewGroup) findViewById(R.id.layout);
View textView = LayoutInflater.from(this).inflate(R.layout.textview, null);
root.addView(textView);
}
}



运行效果图一:

然后,将onCreate()方法中的代码修改为如下:
ViewGroup root = (ViewGroup) findViewById(R.id.layout);
LayoutInflater.from(this).inflate(R.layout.textview, root, true);

ViewGroup root = (ViewGroup) findViewById(R.id.layout);
View textView = LayoutInflater.from(this).inflate(R.layout.textview, root, false);
root.addView(textView);
两种方式中的任意一种。

然后再看运行效果。

运行效果图二:



从图一的显示效果可以看出,我们为TextView设定的layout属性都失去了作用。而在图二中,这三个layout属性都正常显示出来了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐