2011-05-22 36 views
2

我有一個帶有標籤的BorderContainer。我需要這個標籤在容器內居中。 BorderContainer沒有佈局(我猜它是默認的,basicLayout ...)。Flex/as3:如果我設置了BorderContainer的BorderWeight,包含的標籤被移動了......爲什麼?

我的代碼:

使用BorderContainer的定義:

<s:BorderContainer xmlns:fx="http://ns.adobe.com/mxml/2009" 
       xmlns:s="library://ns.adobe.com/flex/spark" 
       xmlns:mx="library://ns.adobe.com/flex/mx" 
       creationComplete="bordercontainer1_creationCompleteHandler(event)" 
       addedToStage="bordercontainer1_addedToStageHandler(event)" 
       cornerRadius="200" borderWeight="20" > 

當我使用BorderContainer完成後,我動態加載標籤:

protected function bordercontainer1_creationCompleteHandler(event:FlexEvent):void 
     { 
      // TODO Auto-generated method stub 
      var countdownText:Label = new Label(); 
       countdownText.width = this.width * 0.5; 
       //countdownText.height = this.height * 0.5; 
       countdownText.text =String(countdownDuration); 
        countdownText.setStyle("fontSize","200"); 
        countdownText.setStyle("fontFamily", "Arial"); 
        countdownText.setStyle("color","#FF0000"); 
        countdownText.setStyle("fontWeight", "bold"); 
        countdownText.setStyle("textAlign", "center");     
       this.addElement(countdownText); 
       //trace("width border:", this.width, ", text width:", countdownText.width); 
       countdownText.x = (this.width-countdownText.width)/2; 
       countdownText.y = (this.width-countdownText.width)/2; 

     } 

有了這個代碼標籤的文本在中心容器,但如果我設置BorderWeight屬性,文本將被移動!!!!!

謝謝先進。

+0

你爲什麼要動態地添加這個文本?這是否有很好的理由? – 2011-05-22 14:13:26

回答

1

所以,我不確定你的意思是「轉移」。我無法用您的代碼重現問題......儘管您的代碼不完整。例如countdownTextbordercontainer1_addedToStageHandler是什麼?

如何避免動態代碼,只使用簡單的數據綁定?

<fx:Declarations> 
    <fx:Number id="countdownDuration">5.123</fx:Number> 
</fx:Declarations> 

<s:BorderContainer cornerRadius="200" borderWeight="20" width="100%" height="100%"> 

    <mx:Label width="50%" text="{countdownDuration}" 
      fontSize="200" fontFamily="Arial" color="#FF0000" 
      fontWeight="bold" textAlign="center" 
      verticalCenter="0" horizontalCenter="0" /> 

</s:BorderContainer> 
相關問題