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

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

问题描述

我要衡量一个查看的间距等。我用这个code为:

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();

这将按预期在Android 4.3模拟器。

This works as expected on an Android 4.3 emulator.

不过在三星Galaxy XCover 2(S7710)的Andr​​oid 4.1.2手机返回测量宽度,是 16777214 ... 我什么都试过,用 View.MEASURED_SIZE_MASK 屏蔽,但没有任何帮助。

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!

推荐答案

传递 WRAP_CONTENT ,而不是实际值 makeMeasureSpec()将没有任何好处,我不认为那些注定要一起使用。

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.

使用的实际约束值作为第一个参数与 MeasureSpec.AT_MOST 模式,这基本上是(父视图或屏幕的尺寸如大小) Math.min() - 与论据是你把规范的价值和看法的预期尺寸

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 将使其使用规范值, MeasureSpec.UNSPECIFIED 将使其使用视图的期望值。所以,如果你没有任何约束(比如,如果你把你的看法变成滚动型)的选项 MeasureSpec.UNSPECIFIED - 以及任何值作为第一个参数

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.

那么, {大师,纠正我,如果我错了} WRAP_CONTENT MeasureSpec.EXACTLY

话虽这么说,试试:

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 好笑的 16777214 ,一起来看看在二进制数的事情:的 https://www.google.ru/search?q=16777214+in+binary 它的 -2 (这是的恒定值 LayoutParams.WRAP_CONTENT ),但截断与逻辑运算(因此没有减)8最显著位,有什么东西在的调用堆栈措施(),这是否:)我不知道这是一个错误。也许应该检查是阴性,抛出异常或某事的说法。

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天全站免登陆