您的位置:首页 > 其它

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

2018-12-14 16:59 253 查看

 

 

转载请注明出处。 https://blog.csdn.net/ruancoder/article/details/52090065

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

关于LayoutInflater对象的获取,参考博文:
https://www.geek-share.com/detail/2677874704.html

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

(1).inflate()方法的使用

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


方法一:

[code]public View inflate(int resource, ViewGroup root) {}


方法二:

[code]public View inflate(int resource, ViewGroup root, boolean attachToRoot) {}


其中方法一最为常见。
常见使用案例一:

[code]View myView = LayoutInflater.from(context).inflate(R.layout.my_view, null);

将布局文件/res/layout/my_view.xml实例化为myView对象。

常见使用案例二:

 
  1. ViewGroup viewRoot;

  2. LayoutInflater.from(context).inflate(R.layout.my_view, viewRoot);

将布局文件/res/layout/my_view.xml实例化的View对象添加到viewRoot布局中。

那么方法一与方法二有什么区别呢?
进入方法一的源码,我们会发现内部调用的其实就是方法二,只是将方法二的第3个参数设为“root != null”。

 
  1. public View inflate(int resource, ViewGroup root) {

  2. return inflate(resource, root, root != null);

  3. }


方法二中的参数和返回值释义:
参数:
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()方法内部。

 
  1. public View inflate(int resource, ViewGroup root, boolean attachToRoot) {

  2. final Resources res = getContext().getResources();

  3. // 根据layout resource id,获取该布局的XmlResourceParser对象

  4. final XmlResourceParser parser = res.getLayout(resource);

  5. try {

  6. return inflate(parser, root, attachToRoot);

  7. } finally {

  8. parser.close();

  9. }

  10. }

从方法内部可以看到,LayoutInflater使用的是pull解析器来解析xml布局文件的。获取到布局resource的XmlResourceParser对象后,接着进入下一个方法。

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

  2.  
  3.  
  4. // ...

  5.  
  6.  
  7. // attrs是传入的布局layout在xml文件里面设置的属性集合

  8. final AttributeSet attrs = Xml.asAttributeSet(parser);

  9.  
  10.  
  11. // 将ViewGroup类型的参数root赋值给result

  12. View result = root;

  13.  
  14.  
  15. // temp是传入的参数resource的根布局View

  16. final View temp = createViewFromTag(root, name, inflaterContext, attrs);

  17.  
  18.  
  19. ViewGroup.LayoutParams params = null;

  20.  
  21.  
  22. // 实例化temp视图内的所有子视图

  23. rInflateChildren(parser, temp, attrs, true);

  24.  
  25.  
  26. if (root != null) {

  27. // 根据attrs属性集,创建布局参数params

  28. params = root.generateLayoutParams(attrs);

  29. // 如果temp不需要添加到root中,那么为temp设置布局参数params

  30. if (!attachToRoot) {

  31. temp.setLayoutParams(params);

  32. }

  33. }

  34.  
  35.  
  36. if (root != null && attachToRoot) {

  37. // 将temp添加到root中,并使用布局参数params

  38. root.addView(temp, params);

  39. }

  40.  
  41.  
  42. if (root == null || !attachToRoot) {

  43. // 将temp赋值给result(在此之前,result==root)

  44. result = temp;

  45. }

  46.  
  47.  
  48. // ...

  49.  
  50.  
  51. return result;

  52. }

该方法是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。

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout

  3. xmlns:android="http://schemas.android.com/apk/res/android"

  4. android:id="@+id/layout"

  5. android:layout_width="match_parent"

  6. android:layout_height="match_parent"

  7. android:orientation="vertical"/>


新建一个textview.xml。这里故意给TextView的layout_width和layout_height设定了固定数值,并添加layout_gravity属性。方便后面看出差异。

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="230dp"

  4. android:layout_height="80dp"

  5. android:layout_gravity="center_horizontal"

  6. android:background="#999999"

  7. android:text="blog.csdn.net/ruancoder"

  8. android:textColor="#ffffff"

  9. android:textSize="18sp"/>


在MainActivity的onCreate()方法中,添加如下代码。

 
  1. <pre name="code" class="java">package net.csdn.blog.ruancoder;

  2.  
  3. import android.app.Activity;

  4. import android.os.Bundle;

  5. import android.view.LayoutInflater;<
    7ff7
    /code>

  6. import android.view.View;

  7. import android.view.ViewGroup;

  8.  
  9. public class MainActivity extends Activity {

  10. @Override

  11. protected void onCreate(Bundle savedInstanceState) {

  12. super.onCreate(savedInstanceState);

  13. setContentView(R.layout.activity_main);

  14.  
  15. ViewGroup root = (ViewGroup) findViewById(R.id.layout);

  16. View textView = LayoutInflater.from(this).inflate(R.layout.textview, null);

  17. root.addView(textView);

  18. }

  19. }

 

 

运行效果图一: 然后,将onCreate()方法中的代码修改为如下:

 
  1. ViewGroup root = (ViewGroup) findViewById(R.id.layout);

  2. LayoutInflater.from(this).inflate(R.layout.textview, root, true);

 
  1. ViewGroup root = (ViewGroup) findViewById(R.id.layout);

  2. View textView = LayoutInflater.from(this).inflate(R.layout.textview, root, false);

  3. root.addView(textView);

两种方式中的任意一种。

然后再看运行效果。

运行效果图二:

从图一的显示效果可以看出,我们为TextView设定的layout属性都失去了作用。而在图二中,这三个layout属性都正常显示出来了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: