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时,默认attachToRoot为fasle
root 不是null时,默认attachToRoot为true
接下来我们只研究三个参数的方法,及各参数的作用
resource 要加载的xml资源id
root attachToRoot为true时,表示View的父容器
attachToRoot为false时,单纯地返回一系列View的布局参数对象
attachToRoot 是否将View添加到root父容器中
如果是false,root只用来生成正确的关于xml中父容器的关于布局参数的子类
看下图源码
重点补充:
在开发过程中,给控件指定的layout_width和layout_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>
1、root不为null,attachToRoot为true
@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中,又一次添加就会报错!(不解决了,直接往下看了)
2、root不为null,attachToRoot为false
@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最外层布局属性起作用了!
3、root为null
@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布局的最外层属性不起作用了!
- inflate方法参数总结
- 【Android】【Inflate】inflate方法的三个参数
- Android inflate方法总结
- inflate()方法参数的含义
- LayoutInflater的inflate方法参数
- <Android>inflate的方法参数说明
- LayoutInflater.inflate()方法的ViewGroup参数问题
- Android LayoutInflater.inflate()方法的参数用途
- 1.三个参数的inflate方法
- LayoutInflater的inflate()方法参数问题
- Android LayoutInflater.inflate()方法参数详解
- Android:inflate.inflate()方法
- Android中inflate方法的用法总结
- inflate方法两个参数和三个参数的区别
- inflate方法
- inflate方法
- 关于LayoutInflater 的inflate方法参数的含义
- LayoutInflater——inflate方法不同参数的区别
- weblogic集群配置和session共享(12c版本) 上
- HTML
- VS2010打开水晶报表乱码问题
- 杂七杂八,随时补充
- Map的迭代简单写法
- inflate方法参数总结
- go写csv文件
- activeMQ环境搭建详解
- JavaScript 闭包使用示例,安全计数
- C++里的强制类型转换符reinterpret_cast、static_cast 、dynamic_cast、const_cast 区别
- BIO、NIO、AIO简析Java版
- 【Python】if condition: continue
- centos7安装jira
- 最小割 [HNOI 2013]切糕