Horizontally aligning tabs within a TabBar control in Flex

The following example shows how you can align tabs within a Flex TabBar control by setting the horizontalAlign style.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/19/horizontally-aligning-tabs-within-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_horizontalAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalAlign:">
                <mx:ComboBox id="comboBox">
                    <mx:dataProvider>
                        <mx:Array>
                            <mx:Object label="left" />
                            <mx:Object label="center" />
                            <mx:Object label="right" />
                        </mx:Array>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:Box backgroundColor="haloSilver">
        <mx:TabBar id="tabBar"
                dataProvider="{arr}"
                width="600"
                tabWidth="100"
                tabHeight="40"
                horizontalAlign="{comboBox.selectedItem.label}"
                direction="horizontal" />
    </mx:Box>

</mx:Application>

View source is enabled in the following example.

You can also set the horizontalAlign style in an external .CSS file or <mx:Style /> block, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/19/horizontally-aligning-tabs-within-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_horizontalAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Style>
        TabBar {
            horizontalAlign: center;
            tabWidth: 100;
            tabHeight: 40;
        }

    </mx:Style>

    <mx:Array id="arr">
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:Box backgroundColor="haloSilver">
        <mx:TabBar id="tabBar"
                dataProvider="{arr}"
                width="600"
                direction="horizontal" />
    </mx:Box>

</mx:Application>

Or, you can set the horizontalAlign style using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/19/horizontally-aligning-tabs-within-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_horizontalAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Script>
        <![CDATA[
            import mx.events.ListEvent;

            private function comboBox_change(evt:ListEvent):void {
                tabBar.setStyle("horizontalAlign", comboBox.selectedItem.label);
            }
        ]]>
    </mx:Script>

    <mx:Array id="arr">
        <mx:Object label="Button" />
        <mx:Object label="ButtonBar" />
        <mx:Object label="ColorPicker" />
        <mx:Object label="ComboBox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalAlign:">
                <mx:ComboBox id="comboBox"
                        change="comboBox_change(event);">
                    <mx:dataProvider>
                        <mx:Array>
                            <mx:Object label="left" />
                            <mx:Object label="center" />
                            <mx:Object label="right" />
                        </mx:Array>
                    </mx:dataProvider>
                </mx:ComboBox>
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:Box backgroundColor="haloSilver">
        <mx:TabBar id="tabBar"
                dataProvider="{arr}"
                width="600"
                tabWidth="100"
                tabHeight="40"
                direction="horizontal" />
    </mx:Box>

</mx:Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/03/19/horizontally-aligning-tabs-within-a-tabbar-control-in-flex/ -->
<mx:Application name="TabBar_horizontalAlign_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <mx:Script>
        <![CDATA[
            import mx.containers.BoxDirection;
            import mx.containers.Box;
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.TabBar;
            import mx.controls.ComboBox;
            import mx.events.ListEvent;

            private var arr:Array;
            private var comboBox:ComboBox;
            private var tabBar:TabBar;

            private function init():void {
                arr = [];
                arr.push({label:"Button"});
                arr.push({label:"ButtonBar"});
                arr.push({label:"ColorPicker"});
                arr.push({label:"ComboBox"});

                var dp:Array = [];
                dp.push({label:"left"});
                dp.push({label:"center"});
                dp.push({label:"right"});

                comboBox = new ComboBox();
                comboBox.dataProvider = dp;
                comboBox.addEventListener(ListEvent.CHANGE,
                            comboBox_change);

                var formItem:FormItem = new FormItem();
                formItem.label = "horizontalAlign:";
                formItem.addChild(comboBox);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem);

                var appControlBar:ApplicationControlBar;
                appControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0)

                tabBar = new TabBar();
                tabBar.dataProvider = arr;
                tabBar.width = 600;
                tabBar.setStyle("tabWidth", 100);
                tabBar.setStyle("tabHeight", 40);
                tabBar.direction = BoxDirection.HORIZONTAL;

                var box:Box = new Box();
                box.setStyle("backgroundColor", "haloSilver");
                box.addChild(tabBar);
                addChild(box);
            }

            private function comboBox_change(evt:ListEvent):void {
                tabBar.setStyle("horizontalAlign",
                            comboBox.selectedItem.label);
            }
        ]]>
    </mx:Script>

</mx:Application>