getMeasuredWidth 返回完全错误的值 [英] getMeasuredWidth returns totally wrong value

查看:27
本文介绍了getMeasuredWidth 返回完全错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须测量一个 View 以与其他人保持间距.我为此使用此代码:

ViewGroup view = ...;view.setPadding(10, 0, 10, 0);int wrapContent = RelativeLayout.LayoutParams.WRAP_CONTENT;int specWidth = MeasureSpec.makeMeasureSpec(wrapContent, MeasureSpec.AT_MOST);视图.测量(规格宽度,规格宽度);int questionWidth = view.getMeasuredWidth();

这在 Android 4.3 模拟器上按预期工作.

然而,三星 Galaxy XCover 2 (S7710) Android 4.1.2 手机返回的测量宽度为 16777214...我尝试了所有方法,使用 View.MEASURED_SIZE_MASK 进行屏蔽,但没有任何帮助.

你能提供一些帮助吗?提前致谢!

解决方案

WRAP_CONTENT 而不是实际值传递给 makeMeasureSpec() 不会有任何好处,我不要认为这些应该一起使用.

MeasureSpec.AT_MOST模式下使用实际约束值作为第一个参数(例如父视图的大小或屏幕的尺寸),基本上是Math.min()代码> -- 参数是您在规范中放置的值和视图所需的维度.

MeasureSpec.EXACTLY 将使其使用规范值,MeasureSpec.UNSPECIFIED 将使其使用视图所需的值.因此,如果您没有任何约束(例如,如果您将视图放入 ScrollView),您的选项是 MeasureSpec.UNSPECIFIED -- 并且任何值作为第一个参数.

所以,{大师,如果我错了,请纠正我}你会在 MeasureSpec.EXACTLY 之外的任何模式下得到 WRAP_CONTENT 行为/p>

话虽如此,请尝试:

ViewGroup view = ...;view.setPadding(10, 0, 10, 0);//要么这个int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);//或这个int specWidth = MeasureSpec.makeMeasureSpec(0/* any */, MeasureSpec.UNSPECIFIED);视图.测量(规格宽度,规格宽度);int questionWidth = view.getMeasuredWidth();

PS16777214 的趣事,看看二进制数:https://www.google.ru/search?q=16777214+in+binary 它是 -2(这是LayoutParams.WRAP_CONTENT 的常量值),但 8 个最高有效位被逻辑运算截断(因此没有减号),measure() 的调用堆栈中有一些东西可以做到这一点:) 我想知道这是否是一个错误.也许它应该检查参数是否为负数并抛出异常或其他什么.

I have to measure a View for spacing others. I use this code for that:

ViewGroup view = ...;
view.setPadding(10, 0, 10, 0);
int wrapContent = RelativeLayout.LayoutParams.WRAP_CONTENT;
int specWidth = MeasureSpec.makeMeasureSpec(wrapContent, MeasureSpec.AT_MOST);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();

This works as expected on an Android 4.3 emulator.

However the returned measured width on a Samsung Galaxy XCover 2 (S7710) Android 4.1.2 phone, is 16777214... I tried everything, masking with View.MEASURED_SIZE_MASK, but nothing helped.

Could you provide some help with this? Thanks in advance!

解决方案

Passing WRAP_CONTENT instead of an actual value to makeMeasureSpec() won't do any good, I don't think those were meant to be used together.

Use actual constraint value as first parameter (e.g. size of the parent view or dimensions of the screen) with the MeasureSpec.AT_MOST mode, which is basically Math.min() -- with arguments being the value you put in the spec and the view's desired dimension.

MeasureSpec.EXACTLY will make it use the spec value, MeasureSpec.UNSPECIFIED will make it use the view's desired value. So if you don't have any constraints (like if you put your view into a ScrollView) your option is MeasureSpec.UNSPECIFIED -- and any value as first argument.

So, {gurus, correct me if I'm wrong} you get WRAP_CONTENT behavior with any mode other than MeasureSpec.EXACTLY

That being said, try:

ViewGroup view = ...;
view.setPadding(10, 0, 10, 0);
// Either this
int specWidth = MeasureSpec.makeMeasureSpec(parentWidth, MeasureSpec.AT_MOST);
// Or this
int specWidth = MeasureSpec.makeMeasureSpec(0 /* any */, MeasureSpec.UNSPECIFIED);
view.measure(specWidth, specWidth);
int questionWidth = view.getMeasuredWidth();

PS Funny thing about 16777214, take a look at the number in binary: https://www.google.ru/search?q=16777214+in+binary It's -2 (which is the constant value of LayoutParams.WRAP_CONTENT) but with 8 most significant bits truncated with a logical operation (hence no minus), there's something in call stack of measure() that does this :) I wonder if this is a bug. Maybe it should be checking the argument for being negative and throwing an exception or something.

这篇关于getMeasuredWidth 返回完全错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆