O'Reilly logo

Programming Flex 2 by Joey Lott, Chafic Kazoun

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding Styling Support

One of the key features of Flex that Flash Player does not inherently support is styles. As we discussed earlier in the book, styles in Flex are a robust mechanism for defining component styles on an instance, class, or global basis, within MXML, ActionScript, and CSS. Styling support is built into the Flex framework and is exposed to custom components that inherit from UIComponent. Because of this, the complexity for integrating styling support for our components is greatly reduced.

To add support for styles you need to add a style metadata tag and override the styleChanged( ) method. After you do that, you can use the getStyle( ) utility method from within your component to retrieve the value of the style. In this section, we will build the code to add a horizontalGap style that will control the space between the icon and the label in our instant messenger status icon component.

First, you need to define the style metadata tag by preceding the class declaration, specifying the style's name and type, and usually disable inheritance:

[Style(name="horizontalGap",type="int", inheriting="false")]
public class StatusIcon extends UIComponent
{

Next, you need to implement the styleChanged( ) method:

override public  function styleChanged(styleProp:String):void
{
    super.styleChanged(styleProp);
    if(styleProp == "horizontalGap")
    {
        invalidateSize( );
        invalidateDisplayList( );
    }
}

In styleChanged( ), we first call super.styleChanged( ), passing it the styleProp value. Then we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required