inflate方法参数总结

来源:互联网 发布:php的switch case语句 编辑:程序博客网 时间:2024/06/07 12:38

inflate方法是LayoutInflater的方法,用来使用xml资源文件创建view对象,一般这样使用:

LayoutInflater.from(viewGroup.getContext()).inflate...

我们来比较一下两种重载方法

inflate(@LayoutRes int resource, @Nullable ViewGroup root)inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)

先来看看源码的注释


root null时,默认attachToRootfasle

root 不是null时,默认attachToRoottrue


接下来我们只研究三个参数的方法,及各参数的作用


resource 要加载的xml资源id

root   attachToRoottrue时,表示View的父容器

         attachToRootfalse时,单纯地返回一系列View的布局参数对象

attachToRoot  是否将View添加到root父容器中

        如果是falseroot只用来生成正确的关xml中父容器的关于布局参数的子类


看下图源码




重点补充:

在开发过程中,给控件指定的layout_widthlayout_height到底是什么意思呢?

该系列属性表示一个控件在容器中的大小,也就是这个控件必须在容器中,这两个值才有意义,否则无意义!


接下来我们举例看下参数各情况下的使用结果:(以RecyclerView中ItemView为例)

RecyclerView中的item布局

//此处注意,我在item布局中,最外层高度为150,而内部控件加边距才一共100,以此来比较

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="150dp"    android:background="#789452"    android:orientation="vertical">    <ImageView        android:id="@+id/iv_recycler_img"        android:layout_width="80dp"        android:layout_height="80dp"        android:layout_marginTop="10dp"        android:layout_marginBottom="10dp"        android:layout_centerVertical="true"        android:layout_marginLeft="15dp"        android:background="#125476"        android:scaleType="center" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_marginLeft="15dp"        android:layout_toRightOf="@id/iv_recycler_img"        android:text="daggas"        android:textColor="#ffffff"        android:textSize="20sp" /></RelativeLayout>

1root不为nullattachToRoottrue

@Overridepublic MasonryView onCreateViewHolder(ViewGroup viewGroup, int i) {    View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview, viewGroup, true);    return new MasonryView(view);}

root不为null,attachToRoot为true时,表示将resource指定的布局添加到root中,添加的过程中resource所指定的的布局的根节点的各个属性都是有效的。

结果崩溃了!

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.


原因是这是在RecyclerView的Adapter中创建ViewHolder时调用,默认创建的view就会添加在viewGroup中,又一次添加就会报错!(不解决了,直接往下看了)


2root不为nullattachToRootfalse

@Overridepublic MasonryView onCreateViewHolder(ViewGroup viewGroup, int i) {    View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview, viewGroup, false);    return new MasonryView(view);}

Item布局根节点属性值有效,又不想让其处于某一个容器中然而在Adapter中,attachToRoot为false,也将view添加在了ViewGroup中),看下效果:


我们能够明显看到item高度为150,显然item最外层布局属性起作用了!


3rootnull

@Overridepublic MasonryView onCreateViewHolder(ViewGroup viewGroup, int i) {    View view = LayoutInflater.from(context).inflate(R.layout.item_recyclerview, null, false);    return new MasonryView(view);}

root为null时,不论attachToRoot为true还是为false,显示效果都是一样的。当root为null表示我不需要将第一个参数所指定的布局添加到任何容器中,同时也表示没有任何容器来协助第一个参数所指定布局的根节点生成布局参数。


看下效果:


 明显看到item布局的最外层属性不起作用了!