`

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一样。

0
0
分享到:
评论

相关推荐

    LayoutInflater.from(context).inflate()方法的调研

    在Android开发中,`LayoutInflater.from(context).inflate()`方法是一个至关重要的组件,用于将XML布局文件转换为视图对象并添加到视图层次结构中。这个方法广泛应用于动态加载和构建用户界面,尤其在处理列表视图、...

    博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码

    博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965

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

    Android 中LayoutInflater.inflate()方法的介绍 Android 中LayoutInflater.inflate()方法是Android开发中最常用的方法之一,用于将布局文件转换成View对象。该方法是LayoutInflater类中的一个成员方法,主要用于将...

    Android LayoutInflater.inflate()详解及分析

    Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google

    Android LayoutInflater.inflate源码分析

    `LayoutInflater.inflate()` 方法是我们经常使用的一个核心方法,尤其是在创建自定义Adapter或动态加载布局时。接下来我们将深入探讨`LayoutInflater.inflate()` 的源码,以理解其工作原理和不同参数版本之间的差异...

    Android中LayoutInflater.inflater()的正确打开方式

    LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...

    Android getViewById和getLayoutInflater().inflate()的详解及比较

    `inflate()` 方法是 `LayoutInflater` 的核心方法,它负责解析XML布局并创建对应的视图层次结构。例如: ```java LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); View ...

    Android代码-可高度定制样式的安卓日历控件

    return LayoutInflater.from(parent.getContext()).inflate(R.layout.view_calendar_item, parent, false); } // @Override public void setDate(View itemVew, int year, int month, int day, boolean ...

    LayoutInflater源码分析 inflate方法的区别

    本篇文章将深入探讨`LayoutInflater`的`inflate`方法之间的区别,帮助开发者更好地理解和利用这个强大的功能。 首先,`LayoutInflater`有两个主要的`inflate`方法: 1. `inflate(@LayoutRes int resource, ...

    LayoutInflater inflate 示例demo

    本示例demo是专为新手设计的,旨在帮助开发者理解`inflate()`方法的不同用法及其参数的含义。通过下载并导入到Android Studio,你可以直接运行此项目,直观地看到`inflate()`的各种使用场景。 首先,`...

    LayoutInflater inflate例子

    `inflate()` 方法是`LayoutInflater` 的核心功能,用于将XML布局资源动态地加载到活动中。下面将详细解释`LayoutInflater` 及其`inflate()` 方法的工作原理,以及如何在实际应用中灵活使用它们。 `LayoutInflater` ...

    Android 中LayoutInflater的使用

    2. **使用方法**:通过`LayoutInflater.from(Context)`获取LayoutInflater实例,然后调用`inflate()`方法加载布局。 3. **inflate()方法**:接受两个参数,第一个是XML布局资源ID,第二个是父视图,用于确定布局的...

    Android LayoutInflater加载布局详解及实例代码

    3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...

    android中LayoutInflater的使用.pdf

    这两种方法在本质上是相同的,因为`LayoutInflater.from(Context context)`实际上就是调用了`getSystemService()`,并传入`Context.LAYOUT_INFLATER_SERVICE`作为参数来获取`LayoutInflater`服务。 `...

    LayoutInflater的使用

    LayoutInflater 的inflate 方法共有四种形式,目的都是把 xml 表述的 layout 转化为 View 对象。其中一个比较常用的是: ```java View inflate(int resource, ViewGroup root) ``` 其中,int resource 是 resource...

    Android LayoutInflater中 Inflate()方法应用

    `inflate()` 方法是`LayoutInflater` 的核心方法,它的作用是解析XML布局文件,并将其转换为Android视图层次结构。下面将详细阐述`inflate()` 方法的用法及其与其他方法的区别。 首先,`inflate()` 方法的基本使用...

Global site tag (gtag.js) - Google Analytics