在Flex中,如何在运行时获取无子画布组件的尺寸?

时间:2022-08-22 21:25:41

One of my components looks like this:

我的一个组件看起来像这样:

<mx:Canvas id="grid" width="100%" height="100%"></mx:Canvas>

On creationComplete, I load some spirte that I want to scale and position based on the dimensions of the canvas to create a custom grid layout, but when I access the dimensions of 'grid' I get 0 and 0. Is there any way to get the dimensions without assigning absolute values?

在creationComplete上,我加载了一些我希望根据画布的尺寸进行缩放和定位以创建自定义网格布局的spirte,但是当我访问'grid'的尺寸时,我得到0和0.有什么办法可以获得尺寸没有指定绝对值?

4 个解决方案

#1


I can't check this at the moment but can you access the measuredHeight and measuredWidth?

我现在无法检查这个,但你可以访问measuredHeight和measuredWidth吗?

#2


My understanding is that unless the Canvas contains one or more DisplayObject children, it will always report its width and height properties as 0, regardless of the percentage sizings you may have applied to it.

我的理解是,除非Canvas包含一个或多个DisplayObject子项,否则它将始终将其width和height属性报告为0,而不管您可能应用于它的百分比大小。

You could always add an empty dummy DisplayObject to the Canvas, but that wouldn't be very elegant. Depending on how you've planned to implement your custom grid, it's possible you'll have to rethink the design...

您总是可以向Canvas添加一个空的虚拟DisplayObject,但这不会很优雅。根据您计划实施自定义网格的方式,您可能需要重新考虑设计......

#3


Is it possibly created but not yet displayed (e.g. visible)? Since final size and shape is derived, it doesn't happen until the thing actually needs to be drawn. Width and height are documented to be the values actually in use - there are even events for when they change.

是否可能已创建但尚未显示(例如可见)?由于最终的大小和形状是派生的,所以直到实际需要绘制它才会发生。宽度和高度被记录为实际使用的值 - 甚至有更改时的事件。

Worst case, try trapping it out in the canvas Resize event.

最糟糕的情况是,尝试在画布Resize事件中捕获它。

#4


have you tried binding the width and height of the Canvas to its parent? If it is 100%, than it will have the same size as its parent and you could either bind or size your Sprite (UIComponent) based on the parent container of the Canvas.

您是否尝试将Canvas的宽度和高度绑定到其父级?如果它是100%,那么它将具有与其父级相同的大小,您可以根据Canvas的父容器绑定或调整Sprite(UIComponent)的大小。

#1


I can't check this at the moment but can you access the measuredHeight and measuredWidth?

我现在无法检查这个,但你可以访问measuredHeight和measuredWidth吗?

#2


My understanding is that unless the Canvas contains one or more DisplayObject children, it will always report its width and height properties as 0, regardless of the percentage sizings you may have applied to it.

我的理解是,除非Canvas包含一个或多个DisplayObject子项,否则它将始终将其width和height属性报告为0,而不管您可能应用于它的百分比大小。

You could always add an empty dummy DisplayObject to the Canvas, but that wouldn't be very elegant. Depending on how you've planned to implement your custom grid, it's possible you'll have to rethink the design...

您总是可以向Canvas添加一个空的虚拟DisplayObject,但这不会很优雅。根据您计划实施自定义网格的方式,您可能需要重新考虑设计......

#3


Is it possibly created but not yet displayed (e.g. visible)? Since final size and shape is derived, it doesn't happen until the thing actually needs to be drawn. Width and height are documented to be the values actually in use - there are even events for when they change.

是否可能已创建但尚未显示(例如可见)?由于最终的大小和形状是派生的,所以直到实际需要绘制它才会发生。宽度和高度被记录为实际使用的值 - 甚至有更改时的事件。

Worst case, try trapping it out in the canvas Resize event.

最糟糕的情况是,尝试在画布Resize事件中捕获它。

#4


have you tried binding the width and height of the Canvas to its parent? If it is 100%, than it will have the same size as its parent and you could either bind or size your Sprite (UIComponent) based on the parent container of the Canvas.

您是否尝试将Canvas的宽度和高度绑定到其父级?如果它是100%,那么它将具有与其父级相同的大小,您可以根据Canvas的父容器绑定或调整Sprite(UIComponent)的大小。