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

所属分类: 软件编程 / Android 阅读数: 33
收藏 0 赞 0 分享

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, itemGroup, 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, itemGroup, 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异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多