Flex 3 - 获取组件的实际尺寸

发布于 2024-10-13 11:08:59 字数 910 浏览 1 评论 0原文

我想在添加一些子项后获得组件的实际高度。

在调试模式下检查变量选项卡时,我看到两个不同的高度变量: $height(包含正确值:138)和 height/_height(包含错误值:10)。

我猜想显示没有更新,所以我没有使用 component.height 获得正确的高度,但是如何获得 $height 中的值?

感谢您提供的任何帮助=)

问候, BS_C3


@Flextras

再次非常感谢您的回答!这真的很详细=)

Measuredheight 没有给我我正在寻找的值,并且explicitHeight 没有值(NaN)。我现在无法测试,所以我无法判断 unscaledHeight...

这是我所拥有的结构:

MainContainer - Canvas
    BOX1 - Canvas (height = 100)
    BOX2 - Canvas (height = 100)
    VariableBox - Canvas (height: depends on the height of it's variable number of children)
    Text - TextArea

我需要 VariableBox 的高度才能定位文本。

该函数如下所示:

updateDisplay(){
    for(i;i<list.length;i++){
        VariableBox.addChild(new HBox);
    }
    // reposition Text depending on VariableBox's height
}

我知道我没有提供任何代码,但我现在不在我的工作计算机上>_<

希望这有帮助!

I'd like to get the actual height of a component after adding some children.

When inspecting the variables tab in debug mode, I see 2 different height variables:
$height (which contains the correct value: 138) and height/_height (which contains a wrong value: 10).

I guess that the display is not updated and so I'm not getting the correct height using component.height, but how can I get the value in $height?

Thanks for any help you can provide =)

Regards,
BS_C3


@Flextras

Again, thanks a lot for your answer! That was really detailed =)

Measuredheight is not giving me the value I'm looking for, and explicitHeight has no value (NaN). I cannot test right now, so I can't tell for unscaledHeight...

Here's the structure of what I have:

MainContainer - Canvas
    BOX1 - Canvas (height = 100)
    BOX2 - Canvas (height = 100)
    VariableBox - Canvas (height: depends on the height of it's variable number of children)
    Text - TextArea

I need the VariableBox's height in order to position Text.

The function looks like this:

updateDisplay(){
    for(i;i<list.length;i++){
        VariableBox.addChild(new HBox);
    }
    // reposition Text depending on VariableBox's height
}

I know I'm not giving any code but I'm not on my working computer right now >_<

Hope this helps!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终难愈 2024-10-20 11:08:59

简短回答:使用 getMeasuredOrExplicitWidth()getMeasuredOrMeasuredHeight() 方法。

在许多情况下,高度width 将返回与这些方法相同的值。

更长的答案:
您使用什么组件?既然您用 Flex 标记了它,我将假设它是一个扩展 UIComponent 的 Flex 视觉组件。

您可以查看一些不同的身高值,但请记住,父母有责任确定孩子的尺寸。向组件添加子组件不一定会改变其高度或宽度。这取决于您要添加子组件的组件是否考虑到子组件。

首先看一下 measuredHeight测量宽度。这些是使用测量方法计算得出的组件的首选尺寸值。某些组件(例如 HBox 或 VBox)会自动“感知”其子组件,并在计算其大小时使用这些组件。其他组件(例如 UIComponent)在设置测量高度或测量宽度时不考虑子组件。

其次,您可以查看explicitHeight 和explicitWidth。如果您设置了高度或宽度,则这些值也会被设置。如果您使用 setActualSize() 方法,我不相信会设置这些值。引擎盖下有一些魔法,因此 explicitHeightexplicitWidth 被隐藏在高度和宽度后面。它们通常是相同的,但并非总是如此。

我通常不处理缩放,但如果您需要组件的未缩放高度和宽度,可以使用 unscaledWidthunscaledHeight 的受保护属性成分。我相信这些是传递给 updateDisplayList() 方法的值。

请记住,要使组件在添加或删除子组件后调整自身大小,该组件必须经历其自己的生命周期。所以这段代码:

trace(container.width);
container.addChild(myChild);
trace(container.width);

可能不会改变容器的宽度,因为容器没有机会运行measure()、updateDisplayList()或commitProperties()方法。

如果这没有帮助,您将必须更具体并提供一些代码。

Short answer: Use the getMeasuredOrExplicitWidth() and getMeasuredOrExplicitHeight() methods.

In many cases, height or width will return the same values as those methods.

Longer answer:
What component are you using? Since you tagged this with Flex, I'm going to assume it is a Flex visual Component that extends UIComponent.

You have a handful of different height values to look at, but keep in mind it is always the responsibility of the parent to size it's children. Adding children to a component will not necessarily change it's height or width. It depends if the component you're adding children to takes children into account, or not.

First take a look at measuredHeight and measuredWidth. These are the preferred size values of the component, calculated using the measure method. Some components, such as an HBox or VBox will automatically be "aware" of their children and will use these when calculating it's size. Other components, such as UIComponent do not take into account children when setting the measuredHeight or measuredWidth.

Second, you can take a look at explicitHeight and explicitWidth. If you set height or width, then these values are set. I do not believe these values are set if you use the setActualSize() method. There is some magic under the hood so that explicitHeight and explicitWidth are masked behind height and width. They are often the same, but not always.

I don't usually deal with scaling, but if you need the unscaled height and width of a component, there is an unscaledWidth and unscaledHeight protected properties of the component. I believe those are the values passed to the updateDisplayList() method.

Keep in mind that for a component to resize itself after adding or removing children, the component must go through a lifecycle of it's own. So this code:

trace(container.width);
container.addChild(myChild);
trace(container.width);

Probably will not change the width of the container, because the container has not had a chance to run the measure(), updateDisplayList(), or commitProperties() methods.

If this doesn't help, you're going to have to get more specific and provide some code.

无法回应 2024-10-20 11:08:59

最终解决方案:

我终于找到了一些令我满意的解决方法,而无需做一些奢侈的事情...

我有一个父容器,我动态地向其中添加一些子容器,并且它的高度没有更新。我遇到的问题是,父容器的measuredHeight、explicitHeight、height和unscaledHeight都没有给我我需要的高度。

因此,我为每个孩子的creationComplete添加了一个监听器。一旦创建完成事件启动,子级的每个控制都会被考虑在内。此时,父容器的measuredHeight被设置为期望值。所以我只需要设置:

parentContainer.height = parentContainer.measuredHeight

再次感谢 Flextras 提供了一些有用的信息 =)

问候。

Final solution:

I finally found some workaround that's satisfying for me without doing some extravagant stuff...

I had a parent container to which I was dynamically adding some children, and it's height wasn't getting updated. The issue I was having is that neither measuredHeight, nor explicitHeight, nor height, nor unscaledHeight of the parent container were giving me the height I needed when I wanted it.

So, I added a listener to the creationComplete of each child. Once the creation complete event is launched, every control of the child is taken into account. At this moment, the measuredHeight of the parent container is set to the expected value. So I just needed to set:

parentContainer.height = parentContainer.measuredHeight

Thanks again to Flextras who has given some useful information =)

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文