2

我有一个带有标签的 BorderContainer。我需要这个标签在容器内居中。BorderContainer 没有布局(我猜它是默认的,basicLayout ...)。

我的代码:

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 属性,则文本会移动!!!!!!

先谢谢了。

4

1 回答 1

1

所以,我不确定你所说的“转移”是什么意思。我无法用您的代码重现问题……尽管您的代码不完整。例如,什么是countdownTextor bordercontainer1_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>
于 2011-05-22T14:36:18.910 回答