Setting horizontal and vertical gaps between buttons in a Flex ButtonBar control

by Peter deHaan on January 8, 2008

in ButtonBar

The following example shows how you can control the horizontal and vertical gaps between buttons in a ButtonBar control in Flex by setting the verticalGap and horizontalGap styles.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/08/setting-horizontal-and-vertical-gaps-between-buttons-in-a-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="The" />
        <mx:Object label="quick" />
        <mx:Object label="brown" />
        <mx:Object label="fox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="verticalGap:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-2"
                        maximum="10"
                        value="0"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            verticalGap="{slider.value}"
            direction="vertical" />

</mx:Application>

View source is enabled in the following example.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/01/08/setting-horizontal-and-vertical-gaps-between-buttons-in-a-flex-buttonbar-control/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="horizontal"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:Array id="arr">
        <mx:Object label="The" />
        <mx:Object label="quick" />
        <mx:Object label="brown" />
        <mx:Object label="fox" />
    </mx:Array>

    <mx:ApplicationControlBar dock="true">
        <mx:Form styleName="plain">
            <mx:FormItem label="horizontalGap:" direction="horizontal">
                <mx:HSlider id="slider"
                        minimum="-2"
                        maximum="10"
                        value="0"
                        liveDragging="true"
                        snapInterval="1"
                        tickInterval="1" />
                <mx:Label text="{slider.value}" />
            </mx:FormItem>
        </mx:Form>
    </mx:ApplicationControlBar>

    <mx:ButtonBar id="buttonBar"
            dataProvider="{arr}"
            horizontalGap="{slider.value}"
            direction="horizontal" />

</mx:Application>

View source is enabled in the following example.

You could also set the horizontalGap or verticalGap style in an external .CSS file or <mx:Style /> block, as seen in the following snippet:

<mx:Style>
    ButtonBar {
        horizontalGap: 16;
    }
</mx:Style>

Or, you can set the horizontalGap or verticalGap style using ActionScript, as seen in the following snippet:

<mx:Script>
    <![CDATA[
        private function init():void {
            buttonBar.setStyle("horizontalGap", 16);
        }
    ]]>
</mx:Script>

{ 2 comments… read them below or add one }

1 kathryn January 17, 2008 at 1:39 pm

this was very helpful, thanks! i always forget that gaps are styles, not properties :-)

Reply

2 Andy March 25, 2009 at 11:34 am

Just spent a few hours trying to adjust the padding of the individual buttons in a button bar inline in actionscript. This works, but it ain’t that pretty:

var buttonStyleObj:CSSStyleDeclaration = new CSSStyleDeclaration("buttonStyleObj");
buttonStyleObj.setStyle("paddingRight", 4);
buttonStyleObj.setStyle("paddingLeft", 4);
StyleManager.setStyleDeclaration(".buttonStyle", buttonStyleObj, true);

var buttonBar:ButtonBar = new ButtonBar();
buttonBar.setStyle("buttonStyleName", "buttonStyle");

Reply

Leave a Comment

Sorry, this blog is terrible at eating HTML comments.
If you're pasting any HTML/XML/MXML code, you need to convert your < characters to &lt; and your > characters to &gt; .

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Anti-Spam Protection by WP-SpamFree

Previous post:

Next post: