Flex:如何调整 DisplayObject 大小以适应 Flex 应用程序中的面板
我试图将一些从 Sprite 继承的 actionscript 类附加到我的 Flex 应用程序,方法是将其附加到 UIComponent,然后将 UIComponent 添加到面板。但是,我的 Sprite 类的大小似乎比面板大。因此,我尝试使用 DisplayObject.scale 属性调整它的大小,但我需要知道容器的大小。因为我试图使我的代码可重用,所以我认为我应该让我的 Sprite 类通过其父属性获取它来处理调整大小(比方说,我可以通过设置
this.scaleX = this.parent.Width/this.宽度或类似的东西)
但是,我的 Sprite 类的父级(即 UIComponent)的宽度/高度为 0。 所以,我的问题是: 1)是否有更好的方法来调整 DisplayObject 类的大小以在附加到 Flex 对象时适合其容器? 2)如果你知道,为什么我的 UIComponent 的大小保持为 0 而我的 DisplayObject 仍然以其完整大小显示?
这是我的代码,使用 Flash Builder 4:
private var loader:Loader = new Loader(); private function testLoader():void { var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png"); loader.load(urlRequest); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete); } private function onLoaderLoadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete); var ui : UIComponent = new UIComponent(); ui.addChild(loader); panelTest.addElement(ui); trace("ui.width = " + ui.width); trace("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 loader.width = 571 loader.height = 411 panelTest.width = 480 panelTest.height = 320
I am trying to attach some of my actionscript class, inherited from Sprite, to my Flex application by attaching it to a UIComponent, then add UIComponent to a panel. However, the size of my Sprite class appears to be larger than the panel. So, I try to resize it using DisplayObject.scale property, but I need to know the size of my container. Since I try to make my code reusable, I figure that I should let my Sprite class handle the resize by getting it via its parent property (let's say, I can resize it by set
this.scaleX = this.parent.Width/this.width or something like this)
However, my Sprite class's parent, which is the UIComponent, has width/height of 0.
So, my question is:
1) Is there any better way to resize DisplayObject class to fit its container when attached to Flex object?
2) If you know, why my UIComponent's size remain 0 and my DisplayObject still appears at its full size?
Here's my code, using Flash Builder 4:
private var loader:Loader = new Loader(); private function testLoader():void { var urlRequest:URLRequest = new URLRequest("http://localhost/welcome.png"); loader.load(urlRequest); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoaderLoadComplete); } private function onLoaderLoadComplete(e:Event):void { loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onLoaderLoadComplete); var ui : UIComponent = new UIComponent(); ui.addChild(loader); panelTest.addElement(ui); trace("ui.width = " + ui.width); trace("ui.height = " + ui.height); trace("loader.width = " + loader.width); trace("loader.height = " + loader.height); trace("panelTest.width = " + panelTest.width); trace("panelTest.height = " + panelTest.height); }
And this is the result when run:
ui.width = 0 ui.height = 0 loader.width = 571 loader.height = 411 panelTest.width = 480 panelTest.height = 320
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于剩下的 0,不幸的是,这是一个已知的(grrr)问题: http: //www.mail-archive.com/[email protected]/msg39998.html
您可以尝试在黑客方式(使用 mx_internal 命名空间): 获取动态创建的自定义 uicomponent 的宽度
将精灵直接添加到面板中也可能会有所帮助。这可以通过将加载程序添加到面板的 rawChildren 来完成: http://www .actionscript.org/forums/showthread.php3?p=728034
尽情享受。
About the remaining 0, unfortunately is a known (grrr) problem: http://www.mail-archive.com/[email protected]/msg39998.html
You can try to access it in a hacked way (using the mx_internal namespace): get width of dynamically created custom uicomponent
Adding the sprite directly to the panel may also be helpful. This can be done by adding the loader to the panel's rawChildren: http://www.actionscript.org/forums/showthread.php3?p=728034
Enjoy.