I have a BorderContainer with a Label inside. I need this Label to be centered inside the container. BorderContainer has no layout (I guess it getst the default one, basicLayout...).
My code:
BorderContainer's definition:
<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" >
When my BorderContainer is completed, I load dynamically the 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;
}
With this code the Label's text is centered in the container,开发者_如何学JAVA but if I set the BorderWeight property, the text is shifted!!!!!
Thanks in advanced.
So, I am not sure what you mean by "shifted". I was not able to reproduce the problem with your code... though your code is incomplete. What is countdownText
or bordercontainer1_addedToStageHandler
, for instance?
How about avoiding the dynamic code and just use a simple data binding?
<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>
精彩评论