Setting the horizontal alignment on a Spark List control in Flex 4

The following example shows how you can set the horizontal alignment on a Spark List control in Flex 4 with a vertical layout by setting the horizontalAlign property on the VerticalLayout object to one of the static constants in the mx.layout.HorizontalAlign class.

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.

For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/25/setting-the-horizontal-alignment-on-a-fxlist-control-in-flex-gumbo/ -->
<s:Application name="Spark_List_layout_VerticalLayout_horizontalAlign_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="horizontalAlign:">
                <s:DropDownList id="comboBox"
                          selectedIndex="4">
                    <s:dataProvider>
                        <s:ArrayList source="[left,center,right,justify,contentJustify]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <s:List id="list"
            alternatingItemColors="[#DFDFDF,#EEEEEE]"
            width="260"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="{comboBox.selectedItem}"
                    requestedRowCount="6" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:String>0. The quick brown fox jumps over the lazy dog</fx:String>
                <fx:String>1. The</fx:String>
                <fx:String>2. Quick</fx:String>
                <fx:String>3. Brown</fx:String>
                <fx:String>4. Fox</fx:String>
                <fx:String>5. Jumps</fx:String>
                <fx:String>6. Over</fx:String>
                <fx:String>7. The</fx:String>
                <fx:String>8. Lazy</fx:String>
                <fx:String>9. Dog</fx:String>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

[GoogleAdsWide]

View source is enabled in the following example.

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

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/02/25/setting-the-horizontal-alignment-on-a-fxlist-control-in-flex-gumbo/ -->
<s:Application name="Spark_List_layout_VerticalLayout_horizontalAlign_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx">
    <s:controlBarContent>
        <mx:Form>
            <mx:FormItem label="horizontalAlign:">
                <s:DropDownList id="comboBox"
                        selectedIndex="4"
                        change="comboBox_changeHandler(event);">
                    <s:dataProvider>
                        <s:ArrayList source="[left,center,right,justify,contentJustify]" />
                    </s:dataProvider>
                </s:DropDownList>
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            import spark.events.IndexChangeEvent;
 
            protected function comboBox_changeHandler(evt:IndexChangeEvent):void {
                VerticalLayout(list.layout).horizontalAlign = comboBox.selectedItem;
            }
        ]]>
    </fx:Script>
 
    <s:List id="list"
            alternatingItemColors="[#DFDFDF,#EEEEEE]"
            width="260"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    requestedRowCount="6" />
        </s:layout>
        <s:dataProvider>
            <s:ArrayList>
                <fx:String>0. The quick brown fox jumps over the lazy dog</fx:String>
                <fx:String>1. The</fx:String>
                <fx:String>2. Quick</fx:String>
                <fx:String>3. Brown</fx:String>
                <fx:String>4. Fox</fx:String>
                <fx:String>5. Jumps</fx:String>
                <fx:String>6. Over</fx:String>
                <fx:String>7. The</fx:String>
                <fx:String>8. Lazy</fx:String>
                <fx:String>9. Dog</fx:String>
            </s:ArrayList>
        </s:dataProvider>
    </s:List>
 
</s:Application>

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.