样式的使用(七)
在自定义组件中使用运行时样式
1、css文件
/* ../assets/CustomComponentStyles.css */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/halo"; .specialStyle{ font-size:24; font-weight:bold; }
记得要把文件编译为swf文件
2.自定义组件MyButton.as
package comps { import mx.events.FlexEvent; import spark.components.Button; public class MyButton extends Button { public function MyButton() { addEventListener(FlexEvent.INITIALIZE,initEventHandler); } private function initEventHandler(event:FlexEvent):void{ styleManager.loadStyleDeclarations("assets/CustomComponentStyles.swf",true); this.styleName="specialStyle"; } } }
3.在组件中使用
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" xmlns:comps="comps.*"> <comps:MyButton label="custom"/> </s:Application>