Setting the Width and Height of an Object in Flex to 100% with Actionscript

Today I had the problem that i needed to create a Flex element by hand and apply to it a width of 100%.

This goes as easy with MXML as this
<mx:VBox id="wrapperBox"><mx:UIComponent width="100%" height="100%" id="elementXYZ"></mx:VBox>

But you cannot code this to achieve the adding programatically with Actionscript:
var elementXYZ:UIComponent = new UIComponent();
elementXYZ.width = "100%";
elementXYZ.height = "100%";
wrapperBox.addChild(elementXYZ);

Instead, you have to use this:
var elementXYZ:UIComponent = new UIComponent();
elementXYZ.percentWidth = 100;
elementXYZ.percentHeight = 100;
wrapperBox.addChild(elementXYZ);

Published by Johannes Luderschmidt

About this blog