http://blog.csdn.net/guolin_blog/article/details/12921889

LayoutInflater主要是用于加载布局的,有两个方法可以获取到LayoutInflater的实例:

第一种,用from方法

LayoutInflater layoutInflater = LayoutInflater.from(context);  

第二种,用context的getSystemService方法

LayoutInflater layoutInflater = (LayoutInflater) context  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);  

第一种其实是第二种的封装

获取实例用,就可以调用他的inflate方法加载布局:

layoutInflater.inflate(resourceId, root);

inflate()方法一般接收两个参数,第一个参数就是要加载的布局id,第二个参数是指给该布局的外部再嵌套一层父布局,如果不需要就直接传null。这样就成功成功创建了一个布局的实例,之后再将它添加到指定的位置就可以显示出来了。

比如想要添加一个按钮:

public class MainActivity extends Activity {  private LinearLayout mainLayout;  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  mainLayout = (LinearLayout) findViewById(R.id.main_layout);  LayoutInflater layoutInflater = LayoutInflater.from(this);  View buttonLayout = layoutInflater.inflate(R.layout.button_layout, null);  mainLayout.addView(buttonLayout);  }  }  

Button在界面上显示出来了!说明我们确实是借助LayoutInflater成功将button_layout这个布局添加到LinearLayout中了。LayoutInflater技术广泛应用于需要动态添加View的时候,比如在ScrollView和ListView中,经常都可以看到LayoutInflater的身影。

源码解析:

inflate源码:

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot) {  synchronized (mConstructorArgs) {  final AttributeSet attrs = Xml.asAttributeSet(parser);  mConstructorArgs[0] = mContext;  View result = root;  try {  int type;  while ((type = parser.next()) != XmlPullParser.START_TAG &&  type != XmlPullParser.END_DOCUMENT) {  }  if (type != XmlPullParser.START_TAG) {  throw new InflateException(parser.getPositionDescription()  + ": No start tag found!");  }  final String name = parser.getName();  if (TAG_MERGE.equals(name)) {  if (root == null || !attachToRoot) {  throw new InflateException("merge can be used only with a valid "  + "ViewGroup root and attachToRoot=true");  }  rInflate(parser, root, attrs);  } else {  View temp = createViewFromTag(name, attrs);  ViewGroup.LayoutParams params = null;  if (root != null) {  params = root.generateLayoutParams(attrs);  if (!attachToRoot) {  temp.setLayoutParams(params);  }  }  rInflate(parser, temp, attrs);  if (root != null && attachToRoot) {  root.addView(temp, params);  }  if (root == null || !attachToRoot) {  result = temp;  }  }  } catch (XmlPullParserException e) {  InflateException ex = new InflateException(e.getMessage());  ex.initCause(e);  throw ex;  } catch (IOException e) {  InflateException ex = new InflateException(  parser.getPositionDescription()  + ": " + e.getMessage());  ex.initCause(e);  throw ex;  }  return result;  }
} 

可以看到,inflate是用pull来解析xml格式的,其中调用了createViewFromTag()这个方法,并把节点名和参数传了进去。看到这个方法名,我们就应该能猜到,它是用于根据节点名来创建View对象的。确实如此,在createViewFromTag()方法的内部又会去调用createView()方法,然后使用反射的方式创建出View的实例并返回。

这里创建的是根布局的实例,接下来会调用rInflate()方法来循环遍历这个根布局下的子元素:

private void rInflate(XmlPullParser parser, View parent, final AttributeSet attrs)  throws XmlPullParserException, IOException {  final int depth = parser.getDepth();  int type;  while (((type = parser.next()) != XmlPullParser.END_TAG ||  parser.getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {  if (type != XmlPullParser.START_TAG) {  continue;  }  final String name = parser.getName();  if (TAG_REQUEST_FOCUS.equals(name)) {  parseRequestFocus(parser, parent);  } else if (TAG_INCLUDE.equals(name)) {  if (parser.getDepth() == 0) {  throw new InflateException("<include /> cannot be the root element");  }  parseInclude(parser, parent, attrs);  } else if (TAG_MERGE.equals(name)) {  throw new InflateException("<merge /> must be the root element");  } else {  final View view = createViewFromTag(name, attrs);  final ViewGroup viewGroup = (ViewGroup) parent;  final ViewGroup.LayoutParams params = viewGroup.generateLayoutParams(attrs);  rInflate(parser, view, attrs);  viewGroup.addView(view, params);  }  }  parent.onFinishInflate();
}

在这里同样是createViewFromTag()方法来创建View的实例,然后还会递归调用rInflate()方法来查找这个View下的子元素,每次递归完成后则将这个View添加到父布局当中。

这样的话,把整个布局文件都解析完成后就形成了一个完整的DOM结构,最终会把最顶层的根布局返回,至此inflate()过程全部结束。

public View inflate(XmlPullParser parser, ViewGroup root, boolean attachToRoot)的第三个参数attachToRoot

1. 如果root为null,attachToRoot将失去作用,设置任何值都没有意义。

2. 如果root不为null,attachToRoot设为true,则会给加载的布局文件的指定一个父布局,即root。

3. 如果root不为null,attachToRoot设为false,则会将布局文件最外层的所有layout属性进行设置,当该view被添加到父view当中时,这些layout属性会自动生效。

4. 在不设置attachToRoot参数的情况下,如果root不为null,attachToRoot参数默认为true。

所有控件的layout_width等属性,都要有一个父布局才能生效,因为这是用来设置view在布局中的大小,而不是view的大小,所以叫layout_width,不是width。

而LinearLayout的layout_width有效是因为,在setContentView()方法中,Android会自动在布局文件的最外层再嵌套一个FrameLayout,id为content

所以叫setContentView(),其实这个方法也是用Layoutlnflater()实现的

转载于:https://www.cnblogs.com/qlky/p/5674975.html

Android-LayoutInflater相关推荐

  1. Android LayoutInflater 源码解析

    在上篇文章中我们学习了setContentView的源码,还记得其中的LayoutInflater吗?本篇文章就来学习下LayoutInflater. @Overridepublic void set ...

  2. Android LayoutInflater详解

    Android LayoutInflater详解 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类 似于findViewById().不同点是LayoutInflater是用来 ...

  3. Android LayoutInflater原理分析,带你一步步深入了解View

    Android视图绘制流程完全解析,带你一步步深入了解View(一) 转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 ...

  4. Android LayoutInflater原理分析,带你一步步深入了解View(一)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/12921889 有段时间没写博客了,感觉都有些生疏了呢.最近繁忙的工作终于告一段落, ...

  5. Android LayoutInflater详解(转)

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  6. Android LayoutInflater源码解析:你真的能正确使用吗?

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 好久没写博客了,最近忙着换工作,没时间写,工作刚定下来.稍后有时间会写一下换工作经历.接下来进入本篇主题,本来没想写LayoutInflater的 ...

  7. Android LayoutInflater原理分析,带你一步步深入了解View(一) 郭霖学习摘要

    2019独角兽企业重金招聘Python工程师标准>>> public class MainActivity extends Activity {//----------------- ...

  8. Android LayoutInflater 动态地添加删除View

    我想实现点击一个按钮(或其他的事件)添加或删除View,网上找到了LayoutInflater这个类. 下面是我自己一些经验: android官网上LayoutInflater的API:http:// ...

  9. android service layoutinflater,[转]Android LayoutInflater详解

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  10. android layoutinflater用法,Android LayoutInflater的用法详解

    相信我们在开发过程中肯定接触过LayoutInflater,比如ListView的适配器里的getView方法里通过LayoutInflater.from(Context).inflater来加载xm ...

最新文章

  1. mysql写入数据乱码问题的解决
  2. linux主机解析虚拟机超时_Linux 内核超时导致虚拟机无法正常启动
  3. 【算法】剑指 Offer 59 - I. 滑动窗口的最大值
  4. laravel 5.2 Auth用户认证教程
  5. 13. 为什么我们会需要 Pod?
  6. 用mobiscroll.js的treelist实现弹出下拉效果
  7. Django03-视图系统views
  8. malloc和free的常识性问题
  9. unity学习之可编程渲染管线 SRP Batcher
  10. wps空白页怎么删除,迅速帮你解决问题
  11. linux wireshark 中文,Wireshark (简体中文)
  12. android蓝牙开启的通知,在Android(蓝牙低功耗)中启用蓝牙特性通知不起作用
  13. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...
  14. 敏捷迭代开发——Time-Boxing时间盒
  15. SpringCloud与微服务Ⅷ --- Hystrix断路器
  16. 服务器最多带多少硬盘,一般服务器的硬盘空间有多大?怎么能给那么多 – 手机爱问...
  17. 25岁到30岁的女生还可以长高么? 非药物
  18. LayoutManager android.support.v7.widget.LinearLayoutManager@6eb337f is already attached to a Recycl
  19. 【个人喜好诗词之一】雨巷
  20. 百度地图setMapStyle

热门文章

  1. window.onload 函数不执行处理
  2. 如何遍历某数据库中的每一个表的总记录数
  3. ★LeetCode(704)——二分查找(JavaScript)
  4. LeetCode(344)——反转字符串(JavaScript)
  5. 【零基础学Java】—Map集合概述(四十三)
  6. 解决Vue的history模式刷新页面出现404的问题
  7. 【Vue】—动态绑定v-bind
  8. python integer怎么用_Python core.integer方法代码示例
  9. 两个员工,一个做事认真但效率低,一个迟到早退但效率高,只能留一个我该留哪个?
  10. 基金投资需要注意什么?