【问题标题】:Flex: How to resize DisplayObject to fit panel in Flex applicationFlex:如何调整 DisplayObject 的大小以适应 Flex 应用程序中的面板
【发布时间】:2010-04-21 04:55:31
【问题描述】:

我正在尝试通过将一些从 Sprite 继承的 actionscript 类附加到我的 Flex 应用程序,方法是将其附加到 UIComponent,然后将 UIComponent 添加到面板。但是,我的 Sprite 类的大小似乎比面板大。所以,我尝试使用 DisplayObject.scale 属性调整它的大小,但我需要知道我的容器的大小。由于我试图使我的代码可重用,我认为我应该让我的 Sprite 类通过其父属性获取它来处理调整大小(假设我可以通过 set 调整它的大小

this.scaleX = this.parent.Width/this.width 或类似的东西)

但是,我的 Sprite 类的父类 UIComponent 的宽度/高度为 0。 所以,我的问题是: 1) 当附加到 Flex 对象时,有没有更好的方法来调整 DisplayObject 类的大小以适合其容器? 2) 如果你知道,为什么我的 UIComponent 的大小仍然为 0,而我的 DisplayObject 仍然显示为完整大小?

这是我的代码,使用 Flash Builder 4:

私有 var loader:Loader = new Loader(); 私有函数 testLoader():void { var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png"); loader.load(urlRequest); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete); } 私有函数 onLoaderLoadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete); var ui : UIComponent = new UIComponent(); ui.addChild(loader); panelTest.addElement(ui); 跟踪(“ui.width =” + ui.width); 跟踪(“ui.height =” + ui.height); trace("loader.width = " + loader.width); trace("loader.height = " + loader.height); trace("panelTest.width = " + panelTest.width); trace("panelTest.height = " + panelTest.height); }

这是运行时的结果:

ui.width = 0 ui.height = 0 装载机宽度 = 571 装载机.高度 = 411 panelTest.width = 480 panelTest.height = 320

【问题讨论】:

    标签: apache-flex actionscript-3 actionscript flex3 uicomponents


    【解决方案1】:
    1. 我认为是相反的:父元素应该在渲染时设置子元素的大小。您可以覆盖 updateDisplayList(unscaledWidth:Number, unscaledHeight:Number) 方法来设置加载器子项的大小。见Advanced Visual Components in ActionScript
    2. 我猜你的ui 的尺寸并不是真的为0。尺寸是在布局和渲染过程之后更新的。所以你必须在将组件添加到阶段后等待布局完成。

    【讨论】:

    • 我已经修改了ui添加到stage后跟踪的代码,如下所示: private function onLoaderLoadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete); ui.addChild(loader); ui.addEventListener(Event.ADDED_TO_STAGE,onUILoadComplete); panelTest.addElement(ui); }
    • 私有函数 onUILoadComplete(e:Event):void { ui.removeEventListener(Event.ADDED_TO_STAGE,onUILoadComplete);跟踪(“ui.width =” + ui.width);跟踪(“ui.height =” + ui.height); trace("loader.width = " + loader.width); trace("loader.height = " + loader.height); trace("panelTest.width = " + panelTest.width); trace("panelTest.height = " + panelTest.height); } 结果还是一样
    【解决方案2】:

    关于剩下的0,不幸的是一个已知的(grrr)问题:http://www.mail-archive.com/flexcoders@yahoogroups.com/msg39998.html

    您可以尝试以黑客方式访问它(使用 mx_internal 命名空间):get width of dynamically created custom uicomponent

    将精灵直接添加到面板也可能会有所帮助。这可以通过将加载器添加到面板的 rawChildren 来完成:http://www.actionscript.org/forums/showthread.php3?p=728034

    享受吧。

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 2016-02-26
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多