LayoutInflater 的 inflate 方法引发的 RelativeLayout 测量方法异常
来源:互联网 发布:vb数值型 编辑:程序博客网 时间:2024/05/16 16:56
LayoutInflater inflate 最终都会执行三个参数的方法
public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) { final Resources res = getContext().getResources(); if (DEBUG) { Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" (" + Integer.toHexString(resource) + ")"); } final XmlResourceParser parser = res.getLayout(resource); try { return inflate(parser, root, attachToRoot); } finally { parser.close(); } }
三个参数的含义想必大家都很熟悉了,平时我们大家在 Adapter 中 getView() 方法 用 LayoutInflater 的时候一般都是这么用:
view = LayoutInflater.from(mContext).inflate(R.layout.xxx, viewGroup, false);
前段时间碰到一个问题,在 adapter 外面通过 getView 方法来获取 view ,然后测量拿到的 view 时候发生了 crash,而且 crash 只发生在系统版本低于 5.0 的机器上
View item = adapter.getView(0, null, null);item.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
接着顺藤摸瓜,查找到 item 根布局为 RelativeLayout 然后找到 onMeasure 方法,找到版本的一些区别,
在低版本上:
高版本上是:
发现问题出在 adapter.getView(0, null, null)
,由于我们传入的 ViewGroup root,
为 null,所以 inflate() 方法并没有生成对应的 LayoutParam ,然后低版本的源码也没有进行判空的处理,知道原因后,我们对代码做一下修改,修复了这个问题。添加代码如下:
View item = adapter.getView(0, null, null);item.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));item.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));int itemHeight = item.getMeasuredHeight();
阅读全文
0 0
- LayoutInflater 的 inflate 方法引发的 RelativeLayout 测量方法异常
- LayoutInflater的inflate方法
- LayoutInflater的inflate方法
- LayoutInflater的inflate方法
- LayoutInflater的inflate方法实例
- 探究LayoutInflater的inflate()方法
- LayoutInflater.inflate()方法的介绍
- LayoutInflater的inflate方法详解
- LayoutInflater的inflate方法详解
- Android:LayoutInflater的inflate方法
- LayoutInflater的inflate方法详解
- LayoutInflater的inflate方法学习
- LayoutInflater的inflate方法参数
- View inflate 方法与 LayoutInflater inflate 方法的区别详解
- LayoutInflater和inflate()方法的用法
- LayoutInflater和inflate()方法的用法
- LayoutInflater和inflate()方法的用法
- LayoutInflater和inflate()方法的用法
- 打tag后的启示
- 在ubuntu下安装最新版本的R软件
- JS代码中如何检验数据类型
- vue 生命周期
- 个人网站创建百度搜索框
- LayoutInflater 的 inflate 方法引发的 RelativeLayout 测量方法异常
- C++和C#相互调用COM组件的方法简介
- 脑电信号不变特征探究 Research on invariant features of EEG signals
- 让Unity3d Keystore记住密码
- 快速幂
- 方法的重载
- 从头到尾讲Webpack-实例篇(多种方法实现)
- 虹膜识别之区域生长法
- Spring定时器