调用 addChild 后 Flex 4 UIComponent 宽度和高度保持为零
我有一个组件 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要更改组件的大小,您应该实现
measure()
方法。但您还应该遵循其他有关创建自定义组件的建议。在您的情况下,您应该在createChildren() 中创建并添加子组件
方法。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 increateChildren()
method.正式的答案是否定的。组件从不负责设置它自己的高度和宽度值;它只负责根据该组件的父组件设置的值来调整其子组件的大小和位置。
如果您在组件中实现了measure()方法,则可以使用它来设置组件的measuredWidth和measuredHeight。本质上,这将是您向父级容器提供的建议,了解正确定位所有子元素并调整其大小所需的理想尺寸。您自己的组件应根据传入的 unscaledWidth 和 unscaledHeight 值在 updateDisplayList() 中调整大小和位置。
大多数情况下,Flex 默认容器将遵循这些调整值;尽管在某些情况下它们不会。例如,如果子容器需要的大小大于父容器的大小。
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.