我有一个带有Label的BorderContainer。我需要这个Label在容器内居中。 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完成后,我动态加载Label:
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;
}
使用此代码,Label的文本在容器中居中,但如果我设置了BorderWeight属性,则文本会被移动!!!!!
先谢谢。
答案 0 :(得分:1)
所以,我不确定你的意思是“转移”了。我无法用你的代码重现问题......虽然你的代码不完整。例如,countdownText
或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>