调用 addChild 后 Flex 4 UIComponent 宽度和高度保持为零

发布于 2024-12-01 07:17:56 字数 565 浏览 2 评论 0原文

我有一个组件 ObjectHolder,它扩展了 UIComponent,其中包含许多子 UIComponents。但是,使用 addChild() 将这些子项添加到 ObjectHolder 后,其宽度和高度仍为零。 UIComponent 是否可以自动扩展到其包含的子组件的大小?

public class ObjectHolder extends UIComponent
{       
    public function ObjectHolder()
    {
        var c1:UIComponent = new UIComponent();
        c1.graphics.beginFill( 0xffffff );
        c1.graphics.drawRect(0, 0, 100, 100);
        addChild( c1 );
        trace( this.width );  // Displays zero
    }
}

I have a component ObjectHolder which extends UIComponent that holds many children UIComponents. However, after using addChild() to add these children to ObjectHolder, its width and height remain zero. Is it possible for the UIComponent to automatically expand to the size of its containing children components?

public class ObjectHolder extends UIComponent
{       
    public function ObjectHolder()
    {
        var c1:UIComponent = new UIComponent();
        c1.graphics.beginFill( 0xffffff );
        c1.graphics.drawRect(0, 0, 100, 100);
        addChild( c1 );
        trace( this.width );  // Displays zero
    }
}

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

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

发布评论

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

评论(2

杯别 2024-12-08 07:17:56

To change the component's size you should implement measure() method. But you should also follow other recommendations on creating custom components. In your case you should create and add child component in createChildren() method.

相思碎 2024-12-08 07:17:56

UIComponent是否可以自动扩展至尺寸
它包含子组件吗?

正式的答案是否定的。组件从不负责设置它自己的高度和宽度值;它只负责根据该组件的父组件设置的值来调整其子组件的大小和位置。

如果您在组件中实现了measure()方法,则可以使用它来设置组件的measuredWidth和measuredHeight。本质上,这将是您向父级容器提供的建议,了解正确定位所有子元素并调整其大小所需的理想尺寸。您自己的组件应根据传入的 unscaledWidth 和 unscaledHeight 值在 updateDisplayList() 中调整大小和位置。

大多数情况下,Flex 默认容器将遵循这些调整值;尽管在某些情况下它们不会。例如,如果子容器需要的大小大于父容器的大小。

Is it possible for the UIComponent to automatically expand to the size
of its containing children components?

The formal answer is no. A component is never responsible for setting it's own height and width values; it is only responsible for sizing and positioning it's children based on the values set by that component's parent.

If you implement a measure() method in your component, you can use it to set the measuredWidth and measuredHeight of your component. This will, in essence, be a suggestion that you provide to your parent's container on what is the ideal size you need to position and size all your child elements properly. Your own components should be sized and positioned in updateDisplayList() based on the unscaledWidth and unscaledHeight values passed in.

Most of the time the Flex default containers will honor these sizing values; although there are situations where they will not be. For example, if the size the child container needs is more than the size of the parent.

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