测量视图渲染之前视图、测量

2023-09-12 04:09:41 作者:孤独、也是一种态度゜

我需要找出多大的观点将其连接到父后。

I need to find out how big a view will be after attaching it to its parent.

我已经重写这个方法:

onMeasure(int, int);

但看起来这个方法被调用只有当我真正添加我的自定义视图是容器中使用:

but it looks like this method is invoked only when I actually add my custom view to it's container using:

addView(myView);

你觉得有一种方式来获得渲染视图本身之前,这些信息?基本上,我需要知道实际工作大小安装之前,而不是附加的观点在所有如果它会采取更多的一定的空间。

Do you think there is a way to get this information before rendering the view itself? Basically I need to know the actuall size before attaching it and not attach the view at all if it would take more certain amount of space.

谁?

推荐答案

如果你想获得的宽度和高度之前,你的活动已经将其添加到它的视图层次,测量和布置它,你将不得不调用措施()查看自己之前调用 getMeasuredWidth() getMeasuredHeight()

If you want to get the width and height before your activity has added it to its view hierarchy, measured and layed it out you will have to call measure() on the View yourself before calling getMeasuredWidth() or getMeasuredHeight().

由于测量的宽度和高度都设置后的措施被称为(这反过来又要求 onMeasure()),他们将这个点之前返回0。你将不得不提供 MeasureSpec s到措施(..),将根据您的需要而有所不同。

As the measured width and height are set after measure has been called (which in turn calls onMeasure()) they will return 0 before this point. You will have to supply MeasureSpecs to measure(..) that will vary depending on your needs.

这让孩子们施加任何限制自己看起来像

The MeasureSpecs that allow the children to impose any constraints themselves look like

int widthMeasureSpec = MeasureSpec.makeMeasureSpec(*some width*, MeasureSpec.EXACTLY);
int heightMeasureSpec = MeasureSpec.makeMeasureSpec(*some height*, MeasureSpec.EXACTLY);

重要的一点。在上面传递的宽度可以是显式的像素或 ViewGroup.LayoutParams.WRAP_CONTENT ViewGroup.LayoutParams.FILL_PARENT。

Important point. The width passed in above can either be explicit px or ViewGroup.LayoutParams.WRAP_CONTENT or ViewGroup.LayoutParams.FILL_PARENT.

它也值得当你的观点是测量实际的目标层次结构将被传递到的MeasureSpec措施()中指出将配置的基于含 ViewGroups 布局逻辑。它可以叫做不止一次时,如果在此父的ViewGroup的上下文中考虑的第一项措施瓦尔斯无效/它自己的布局constraits /任何兄弟儿童的观点的限制。无需等待的ViewGroup 来调用措施()实施任何逻辑依赖于测量大小就难以前(视情况而定),以获得最终的是measuredWidth和放大器;身高从上面的解决方案,但在所有的情况下,我用它符合目的的上述技术,但他们一直相对比较简单。如果你真的需要在上下文中来衡量你应该就在 onLayout()返回,并明确设置的任何更改,然后 requestLayout()做了。

Its also worth noting when your view is measured in the actual destination hierarchy the MeasureSpec that will be passed to measure() will be configured based upon the containing ViewGroups layout logic. It may be called more than once if the first measure vals are not valid when considered in the context of this parent viewGroup / its own layout constraits / the constraints of any sibling child-views. Without waiting for the viewGroup to call measure() before implementing any logic dependent on the measured size it would be hard (depending on the situation) to get the final measuredWidth & height from the above solution, but in all the instances i have used the above technique it has fit the purpose, but they have been relatively simple. If you really do need to measure in context you should probably just do it after onLayout() has returned and explicitly set any changes then requestLayout() again.