2011-12-13 46 views

回答

2

在构建应用程序时,我认为知道我必须使用的空间比知道实际屏幕尺寸更重要。

要获取此信息,我将绑定到Flex框架并使用传递给updateDisplayList()方法的unscaledWidth和unscaledHeight属性。

问题是,为什么你需要检测屏幕尺寸。如果您只是需要使用它们来确定组件的孩子的大小和位置,那么我相信我上面描述的解决方案是最好的。如果您因其他原因需要此信息,请解释。


这是一个方式,大小和标签位置的里面的updateDisplayList它的容器:

protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{ 
super.updateDisplayList(unscaledwidth, unscaledHeight); 
// first position it 
// you could also use this.myLabel.move(x,y) method to position the element 
this.myLabel.x = 0; 
this.myLabel.y = 0; 

// then size it 
// sizing to 100% height/width of container 
this.myLabel.height = unscaledHeight; 
this.myLabel.width = unscaledWidth; 

// or you could also use setActualSize; which is slightly different than setting height and width 
// becaues using setActualSize won't set the explicitHeight/explicitWidth, therefore measure will still 
// execute 
this.myLabel.setActualSize(unscaledWidth, unscaledHeight) 

// or you could set the label to it's measured height and measured width 
this.myLabel.height = this.myLabel.getExplicitOrMeasuredHeight(); 
this.myLabel.width = this.myLabel.getExplicitOrMeasuredWidth(); 

} 
+0

我还不清楚'updateDisplayList()'如何定位组件的子元素。对于多行文本(默认)'标签',必须设置'width'。在线上的各种教程后,我从舞台/屏幕宽度--2 *边距(假设它跨越屏幕的可读宽度)设置此宽度。 updateDisplayList如何在那里工作? – ina

+0

我更新了我的答案,提供了关于如何在updateDisplayList中设置组件大小的示例代码。 – JeffryHouser

+0

我想我还没有理解的是你如何在运行时调整容器大小?看起来这将是不同的屏幕尺寸。 – ina

5

在大多数情况下,它是使用更可靠FlexGlobals.topLevelApplication.widthFlexGlobals.topLevelApplication.height