最近一直想弄明白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一样。

更多相关文章

  1. Android(安卓)ListView+image的使用
  2. android中使用local_manifest.xml添加软件
  3. Android(安卓)P中如何自定义一个系统Service
  4. android 电话拨号器
  5. android添加广告之--admob
  6. Android如何引用其他工程
  7. android(9)_数据存储与访问3_scard_login事例
  8. Android(安卓)UI开发的基础
  9. Android(安卓)Studio Gradle project sync failed

随机推荐

  1. Bluetooth in Android 4.2 and 4.3(三):Enab
  2. 基于命令行模式开发ANDROID应用
  3. android实现图片按任意路径截取
  4. Android使用Sqlite增删改查小Demo
  5. 浅析Android中Handler机制
  6. Android ApiDemos示例解析(193):Views->T
  7. 约束布局ConstraintLayout的使用你知道吗
  8. Android02--debug.keystore的注册信息
  9. Android4: 旋转屏幕时禁止Activity重启
  10. Android反向进度条(ProgressBar)的实现—