Android 中LayoutInflater.inflate()方法的介绍

Android 中LayoutInflater.inflate()方法的介绍,第1张

概述Android中LayoutInflater.inflate()方法介绍最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。

AndroID 中LayoutInflater.inflate()方法的介绍

最近一直想弄明白LayoutInflater对象的inflate方法的用法,今天做了实例。

<linearLayout     androID:ID="@+ID/ll_item_Group"     androID:layout_wIDth="match_parent"     androID:layout_height="200dp"     androID:background="#FF0000"     androID:orIEntation="vertical" >   </linearLayout> 
itemGroup = (linearLayout) findVIEwByID(R.ID.ll_item_Group); 

 这个作为itemGroup对象。

<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent"   androID:orIEntation="vertical" >    <linearLayout     androID:ID="@+ID/vIEw_content"     androID:layout_wIDth="match_parent"     androID:layout_height="100dp"     androID:background="#4169E1"     androID:orIEntation="horizontal" >   </linearLayout>    <relativeLayout     androID:ID="@+ID/vIEw_todo"     androID:layout_wIDth="100dp"     androID:layout_height="match_parent"     androID:background="#00008B" >   </relativeLayout>  </linearLayout> 

 这个作为include引用的vIEw。测试代码如下:(inflater是LayoutInflater对象的实例,获取方法是:inflater = LayoutInflater.from(this),其它两种方法自己百度)

VIEw v1 = inflater.inflate(R.layout.el_include,null); VIEw v3 = inflater.inflate(R.layout.el_include,itemGroup,false);      VIEw v2 = inflater.inflate(R.layout.el_include,itemGroup); VIEw v4 = inflater.inflate(R.layout.el_include,true); 

测试结果是:

1、V1和V3在Activity里显示效果一样,都是itemGroup原来的内容,V1和V3都是R.layout.el_include里的VIEw对象。

2、V2和V4在Activity里显示效果一样,都是itemGroup添加R.layout.el_include里的内容之后的。V2和V4对象都是加了R.layout.el_include的itemGroup。

V2和V4在Activity里显示效果一样说明itemGroup没有改变!

V2和V4在Activity里显示效果一样说明itemGroup发生了改变,都是将R.layout.el_include里的内容添加到了itemGroup之后的VIEw 

那么merge和include的区别是:

include所引用的就是一个独立的VIEw,而merge引用的VIEw必须放到一个VIEwGroup中。如下例: 

<merge xmlns:androID="http://schemas.androID.com/apk/res/androID"   androID:layout_wIDth="match_parent"   androID:layout_height="match_parent" >    <linearLayout     androID:ID="@+ID/vIEw_content"     androID:layout_wIDth="match_parent"     androID:layout_height="100dp"     androID:background="#4169E1"     androID:orIEntation="horizontal" >   </linearLayout>    <relativeLayout     androID:ID="@+ID/vIEw_todo"     androID:layout_wIDth="100dp"     androID:layout_height="match_parent"     androID:background="#800080" >   </relativeLayout>  </merge> 

 R.layout.el_marge 引用必须是这样的:

VIEw v = inflater.inflate(R.layout.el_marge,true); 

 否则报错:<merge /> can be used only with a valID VIEwGroup root and attachToRoot=true

 也就是说:merge是为了减少include里的根VIEwGroup,那么inflate的marge必须放到VIEwGroup中。 

网上也有老说到marge和framelayout,其实我觉得没有联系。就是R.layout.el_marge若不添加一个VIEwGroup中的它里面的元素而已规则和FrameLayout一样。

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

总结

以上是内存溢出为你收集整理的Android 中LayoutInflater.inflate()方法的介绍全部内容,希望文章能够帮你解决Android 中LayoutInflater.inflate()方法的介绍所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1144468.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存