0

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

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

但是,我的 Sprite 类的父类 UIComponent 的宽度/高度为 0。所以,我的问题是:1) 有没有更好的方法来调整 DisplayObject 类的大小以适应其容器当附加到 Flex 对象时?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
4

2 回答 2

0

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

您可以尝试以黑客方式访问它(使用 mx_internal 命名空间):获取动态创建的自定义 uicomponent 的宽度

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

享受。

于 2010-04-24T22:09:11.760 回答
0
  1. 我认为这是相反的:父元素应该在渲染时设置子元素的大小。您可以覆盖updateDisplayList(unscaledWidth:Number, unscaledHeight:Number)方法来设置加载器子项的大小。请参阅ActionScript 中的高级可视组件
  2. 我猜你的ui的大小不是真的为0。大小在布局和渲染过程之后更新。因此,您必须在将组件添加到阶段后等待布局完成。
于 2010-04-21T06:30:27.600 回答