2011-12-13 59 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