使用0x7fffffff调用heightMeasurespec的onMeasure()

埃博麦克

我有一个覆盖onMeasure的自定义视图。

我看到onMeasure()两次被调用,第一次是heightMeasureSpec为0x7fffffff-似乎不是有效的MeasureSpec值。

然后,它再次被调用,此时具有正确的值。

以下是基本组成部分,分为相关部分:

自定义视图:

public class CustomView extends ViewGroup implements SurfaceHolder.Callback {
    public CustomView(Context context, AttributeSet as) {
        super(context, as);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.custom_view, this, true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec);
        int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec);

        setMeasuredDimension(width, height);
    }
}

布局(同样,将其精简以使其尽可能简短):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    />

这是包含它的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >        
            <CustomView
            android:id="@+id/custom_view"
            android:layout_width="@dimen/custom_size"
            android:layout_height="@dimen/custom_size"
            />
        </RelativeLayout>
    </ScrollView>
</LinearLayout>

这里的主要观察结果:

  • 自定义视图具有固定的宽度和高度(自定义大小为128dp)。
  • 该视图本身位于RelativeLayout内部。
  • 所有这些都在ScrollView内部。

在这种情况下,我首先使用0x40000100、0x7fffffff调用onMeasure,然后再次使用0x40000100、0x40000100调用,这是我最初期望的。

也许首先用无效的高度调用onMeasure是完全正常的吗?我可以忽略它,但是我不想在这里添加任何hack。


解答:这是已修复的操作系统中的错误,在此处记录

在平台版本17和更低版本中,RelativeLayout受到测量错误的影响,该测量错误可能导致使用错误的MeasureSpec值来测量子视图。(有关更多详细信息,请参见MeasureSpec.makeMeasureSpec。)将RelativeLayout容器放置在滚动容器(例如ScrollView或Horizo​​ntalScrollView)中时触发。如果将不适合使用MeasureSpec模式进行适当测量的自定义视图放置在RelativeLayout中,则无论如何它都将无声工作,因为RelativeLayout会通过一个非常大的AT_MOST MeasureSpec。

我不会将其发布为答案,Snicolas足够好看这个,所以他值得他接受。

斯尼科拉斯

我使用了这个小的IDE而且,正如您所看到的,您收到的两个值都处于精确模式下。如果我们遵循onMeasure在StackOverFlow最喜欢的引用实现,它们都朝着相同的方向发展:

除了将它们传递给,您无需执行其他任何操作setMeasureDimensions仅当接收到的度量未处于精确模式时,您才可以修改它们。

我想念什么吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

传递值> 0x7FFFFFFF时localtime()崩溃

Java为什么使用(hash和0x7FFFFFFF)%tab.length来确定键的索引?

“&0x7fffffff”在“ int(time.time()* 1000.0)和0x7FFFFFFF”中是什么意思

对于Tmax(0x7fffffff)一个奇怪的问题,为什么(!x)== x?

32位有符号整数除法在PowerPC上给出0x7fffffff作为商

错误:数组的总大小不能超过0x7fffffff个字节

为什么一个int变量的值为'0xffffffff` >> 1!= 0x7fffffff?

需要了解0x7fffffff和0xffffffff80000000在内存地址空间布局方面的含义

在C ++中,如何将32位数字传递给'int'变量,而不会将大于0x7FFFFFFF的值视为负数?

错误1错误C2148:数组的总大小不能超过0x7fffffff字节我在C中得到此错误

在明确调用setLayoutParams之后未设置OnMeasure的heightMeasureSpec

以精确和规格大小0调用的onMeasure()

何时调用View.onMeasure()?

为什么在Android的onMeasure方法之前调用setter / getter方法?

在自定义视图中,onDraw在onMeasure之前被调用

Java:如何使用大于0x7F的字节文字

错误:使用Gas组装时,表达式后为垃圾“ 0x7f”

NullPointerException android.support.v7.widget.RecyclerView.onMeasure的原因

getHeight() 在基于 ARM 的设备上始终为 0 onMeasure

Wine Steam错误:未处理的异常:未实现的函数x3daudio1_7.dll.X3DAudioInitialize以32位代码(0x7b83ba4e)调用

Windows 7蓝屏0x000000f7

javax.net.ssl.SSLException:写入错误:ssl = 0x7f70604080:系统调用期间I / O错误,管道损坏

在 int laravel x.7 上调用成员函数 update()

这个 x86 Hello World 使用来自 _start 的 32 位 int 0x80 Linux 系统调用的解释是什么?

无法跳转或调用0x8000加载的内核

调用0x16时出现分段错误

使用linq调用方法x次

为什么float.MaxValue 0xFF 0xFF 0x7F 0x7F而不是像int那样的0x7F,0xFF,0xFF,0xFF?

AttributeError:dlsym(0x7fc4cfd563b0,add_all_items_to_map):未找到符号;使用C从Python运行Go