In Flex 2.0.1 and higher, you can use runtime CSS. Thus far in the chapter, when we've talked about CSS, we've talked exclusively about CSS that gets compiled into the Flex application. Now we'll look at how you can load and apply CSS at runtime, effectively restyling an application at runtime.
Because Flex styles often require embedded resources such as skin assets, classes, and fonts, Flex runtime CSS must be precompiled into .swf files. When using Flex 2.0.1 you can use the
mxmlc compiler to compile a .css file into an .swf that includes all assets embedded by the .css file. In order to compile the .css file, simply pass the path to the file as the one compiler argument, as in the following example:
The preceding example compiles styles.css into a file called styles.swf. You can then load styles.swf into your Flex applications at runtime without having to include the .css or any of the embedded assets in the main Flex application. That means you can update the styles for an application without having to recompile and redeploy the application itself. All you have to do is update the styles file. Furthermore, you can have many styles files that the user can choose from at runtime.
In order to load CSS at runtime, you use the
StyleManager.loadStyleDeclarations( ) method. The
loadStyleDeclarations( ) method requires at least one parameter--the path to the .swf file containing the CSS:
If you're loading only ...