Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教一下, 为什么对View宽高修改了不要调用super.onMeasure #101

Open
ivemmy opened this issue Jul 18, 2019 · 2 comments
Open

Comments

@ivemmy
Copy link

ivemmy commented Jul 18, 2019

请教一下,在安卓自定义View进阶-分类与流程中有一个,注意

如果对View的宽高进行修改了,不要调用 super.onMeasure( widthMeasureSpec, heightMeasureSpec); 要调用 setMeasuredDimension( widthsize, heightsize); 这个函数。

这是为什么?

@979451341
Copy link

onMeasure是用于测量view的大小,但是你直接修改了,你想要这个view大小是如此,那你为何还要去重新测量呢?
你也说了是自定义View不是VIewGroup,所以你无需考虑重新计算它的子view的大小

@vejei
Copy link

vejei commented Feb 10, 2021

作者的话其实存在误导,你看官方文档就清楚了:

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) is a valid use.

当重写这个方法时,你必须调用setMeasuredDimension(int, int)来保存测量出来的宽和高,如果不调用那就会触发IllegalStateException,直接调用父类的onMeasure(int, int)是可以的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants