Is there any way to set horizontalAlign from Action Script not from mxml?
Actionscript – Horizontal align components inside HBox from Action Script
actionscriptalignmentapache-flexflex3hbox
Related Solutions
To center stuff inside a HBox, add the following attribute to the box in question,
horizontalAlign="center" width="100%"
As for the empty space created by invisible boxes (HBox or VBox), I dont know if there is any way, but I find myself adding this attribute to the invisible box,
height="{secondLevel.visible ? 200 : 0}"
Hope that helps
There is a horizontalAlign
property, and a verticalAlign
property, on the VBox and HBox components (it's inherited from Box). They determine the horizontal and vertical alignment of the component's children.
I generally use the Spacer object, like Sam mentions. But for what you want to do, this will work great.
In MXML you could do something like:
<mx:RadioButtonGroup id="yesNoRadioGroup"/>
<mx:HBox id="containingHBox" width="100%">
<mx:Text id="textElement" width="200" text="lakdfa lkadslkjraklnd kadflk lakdsjlkja lksdlkjdflk jalkdlkjdfslksajdf lkjasdflkjdsalkjds lksdjlkj"/>
<mx:HBox id="rightAlignedHorizontalContent" width="100%" horizontalAlign="right">
<mx:RadioButton id="yesRadio" label="Yes" groupName="yesNoRadioGroup"/>
<mx:RadioButton id="noRadio" label="No" groupName ="yesNoRadioGroup"/>
</mx:HBox>
</mx:HBox>
Note that the HBox with the horizontalAlign
set has to have a width value, otherwise, it will only be wide enough to accommodate the width of its children, in which case alignment is moot.
Here's an AS version:
<mx:Script>
<![CDATA[
import mx.controls.RadioButton;
import mx.controls.RadioButtonGroup;
import mx.controls.Text;
private var containingHBox:HBox;
private var textElement:Text;
private var rightAlignedHorizontalContent:HBox;
private var yesNoRadioGroup:RadioButtonGroup;
private var yesRadio:RadioButton;
private var noRadio:RadioButton;
override protected function createChildren():void
{
super.createChildren();
containingHBox = new HBox();
containingHBox.percentWidth = 100;
textElement = new Text();
textElement.width = 200;
textElement.text = "lakdfa lkadslkjraklnd kadflk lakdsjlkja lksdlkjdflk jalkdlkjdfslksajdf lkjasdflkjdsalkjds lksdjlkj";
rightAlignedHorizontalContent = new HBox();
rightAlignedHorizontalContent.percentWidth = 100;
rightAlignedHorizontalContent.setStyle("horizontalAlign","right");
yesNoRadioGroup = new RadioButtonGroup();
yesRadio = new RadioButton();
yesRadio.label = "Yes";
yesRadio.groupName = "yesNoRadioGroup";
noRadio = new RadioButton();
noRadio.label = "No";
noRadio.groupName = "yesNoRadioGroup";
addChild(containingHBox);
containingHBox.addChild(textElement);
containingHBox.addChild(rightAlignedHorizontalContent);
rightAlignedHorizontalContent.addChild(yesRadio);
rightAlignedHorizontalContent.addChild(noRadio);
}
]]>
</mx:Script>
Best Answer
Try this: