最近一直想弄明白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一样。
相关推荐
LayoutInflater.from(context).inflate()方法的调研
博客《ListView滑动删除实现之一——merge标签与LayoutInflater.inflate()》对应源码,博客地址:http://blog.csdn.net/harvic880925/article/details/45155965
主要介绍了Android LayoutInflater.inflate源码分析的相关资料,需要的朋友可以参考下
Android LayoutInflater.inflate...我尝试在Google官方文档与网络上其他讨论中寻找有关的说明,而后发现许多人不但不清楚LayoutInflater的inflate()方法的细节,而且甚至在误用它。 这里的困惑很大程度上是因为Google
主要介绍了Android 中LayoutInflater.inflate()方法的介绍的相关资料,希望通过本文大家能掌握这部分内容,需要的朋友可以参考下
LayoutInflater在开发中使用频率很高,但是一直没有太知道LayoutInflater.from(context).inflate()的真正用法,今天就看看源码的流程。 首先来看from()的源码: /** * Obtains the LayoutInflater from the ...
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方法的区别
由于本人刚刚学习Android 对于getViewById和getLayoutInflater().inflate()的方法该如何使用不知如何分别,这里就上网查下资料整理下,大家可以看下。 LayoutInflater 要明白这个问题首先要知道什么是...
3.我们定义View的时候,如果需要在布局中使用,则必须实现带AttributeSet参数的构造方法,这又是为什么呢? 既然在这篇文章提出来,那说明这三个问题都是跟LayoutInflater脱不了干系的。在我们的分析过程中,会对...
VaryViewHepler切换不同布局的帮助类初始化helpermVaryViewHelper = new VaryViewHelper.Builder().setDataView(findViewById(R.id.vary_content)).setLoadingView(LayoutInflater.from(this).inflate(R.layout....
LayoutInflater.from(this).inflate(R.layout.tab2, tabHost.getTabContentView(), true); LayoutInflater.from(this).inflate(R.layout.tab3, tabHost.getTabContentView(), true); //添加标签页 tabHost....
}上面的代码块中,我通过LayoutInflater.inflate(,)将Layout文件–layout.list_item转换为View.(注:Layout
Android Inflate()方法的作用是将xml定义的一个布局找出来,但仅仅是找出来而且隐藏的,没有找到的同时并显示功能。最近做的一个项目就是这一点让我迷茫了好几天。 Android上还有一个与Inflate()功能类似的方法叫...
android inflate demo,方便新手使用,下载导入android studio就可直接运行,简单明了的展现inflate不同参数的区别
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template1,null); View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.p_template2,null); viewList.add(view1); ...