更新 UIComponent 大小

发布于 2024-08-23 00:34:14 字数 289 浏览 2 评论 0原文

我创建了一个扩展 UIComponent 的 AS3 类,以便我可以轻松地将其添加到我的 MXML 布局中。它工作正常,但我试图弄清楚如何动态更新 UIComponent 宽度和高度。我希望我的超级 UIComponent 在内容周围绘制边框,因此我需要获取特定的“内容”尺寸才能执行此操作。

我理解 UIComponent 的想法是您应该专门设置宽度和高度...但我实际上只是扩展它,以便我能够轻松地通过 MXML 使用我的自定义组件。

有没有一种方法可以获取 UIComponents“内容”的宽度和高度?

谢谢

I've created a AS3 class which extends UIComponent so that I can easily add it to my MXML layout. It works fine, but I'm trying to figure out how to update the UIComponent width and height dynamically. I want my super UIComponent to draw a border around the content, so I need get the specific 'content' dimensions in order to do this.

I understand the idea of UIComponent is that you are supposed to specifically set the width and height... but I am literally only extending it so that I am able to use my custom component with MXML easily.

Is there a method in which I can get the width and height of a UIComponents 'content'?

Thanks

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

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

发布评论

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

评论(2

面犯桃花 2024-08-30 00:34:14

就像 Robusto 在他的评论中描述的那样,如果您想向其中添加子项,Canvas 可能是更好的选择。 Canvas 实例化一个 CanvasLayout,它处理调整自身和子级大小的所有复杂逻辑。

UIComponent 内容的宽度和高度在 measure() 方法中确定,除非 UIComponent 已定义显式大小或百分比大小。测量是没有详细记录的方法之一。我建议深入了解 Flex 组件生命周期与EffectiveUI。他们深入探讨生命周期方法并真正解释如何使用它们。

如果父级(您的组件)没有定义任何大小调整,则调用 measure() 方法。然后它会遍历所有子项,计算它们的大小,并将它们汇总为 measuredWidthmeasuredHeight。然后将父级的大小设置为这些值。

如果扩展 UIComponent,则必须在 measure 方法中执行大量操作来处理所有边缘情况:显式、百分比、约束(顶部、左侧...)、填充,无论是否 includeInLayout = true 等等。Canvas 可以做到这一切。与其他容器组件相同:Box、HBox、VBox、List 等。

希望有帮助,

Like Robusto described in his comment, the Canvas is probably a better choice if you're trying to add children to it. The Canvas instantiates a CanvasLayout, which handles all the complex logic for sizing itself and children.

The width and height of a UIComponent's content is determined in the measure() method, unless the UIComponent has explicit or percent sizes defined. Measure is one of those methods that aren't well documented. I suggest Diving Deep into the Flex Component Lifecycle with EffectiveUI. They go into the lifecycle methods and really explain how to use them.

The measure() method is called if the parent (your component) doesn't have any sizing defined. Then it goes through all of its children, calculates their sizes, and sums them up into measuredWidth and measuredHeight. The parent's sizes are then set to those values.

If you extend UIComponent, you have to do a lot in the measure method to handle all the edge cases: explicit, percent, constraints (top, left...), padding, whether or not includeInLayout = true, etc. Canvas does all that. Same with the other container components: Box, HBox, VBox, List, etc.

Hope that helps,
Lance

绝對不後悔。 2024-08-30 00:34:14

不确定 UIComponent 的“内容”是什么意思。您是指 UIComponent 的尺寸,还是子容器的尺寸?

您可以查找任何组件的大小的一个地方是其 updateDisplayList 方法,这是一种您必须重写的受保护方法,如下所示:

override protected function updateDisplayList(width:Number, height:Number) {
  super.updateDisplaylist(width, height);
  // your statements here
}

您说您只是扩展 UIComponent,以便您能够使用自定义组件“ MXML 很容易。”但您仍然需要根据需要扩展它,以便完成您需要做的事情。为此,您应该了解组件的生命周期,何时覆盖 createChildren()、commitProperties()、measure() 和其他方法,等等。其实并不难,而且你已经迈出了第一步。祝你好运!

Not sure what you mean by the "content" of your UIComponent. Do you mean the UIComponent's dimensions, or a child container's?

One place you can look for the size of any component is in its updateDisplayList method, which is a protected method you would have to override, as so:

override protected function updateDisplayList(width:Number, height:Number) {
  super.updateDisplaylist(width, height);
  // your statements here
}

You say you are extending UIComponent only so that you are able to use your custom component "with MXML easily." But you still need to extend it as much as you need to in order to do what you need to do. For that you should learn about the lifecycle of a component, when to overrwrite the createChildren(), commitProperties(), measure() and other methods, and so on. It's really not hard, and you've already taken the first step. Good luck!

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