样式的使用(六)

【1】 setStyle()使用的注意事项:

1.setStyle()方法尽量不要使用,不要在第一次使用;

2.第一次使用样式时,尽量设置在<fx:Style>,外部样式表中。

3.如果必须使用setStyle()方法,要在实例化的时候就调用,这时setStyle()方法发生在preinitialize时。

【2】运行时加载样式表

运行时加载样式分为三步:

1.写css样式文件

2.把css文件编译为swf文件

3.在应用程序中调用 StyleManager.loadStyleDeclarations()。

例子:

第一步:写css样式文件

/* assets/BasicStyle.css*/
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/halo";
s|Button {
	fontSize: 24;
	color: #FF9933;
}
s|Label {
	fontSize: 24;
	color: #FF9933;
}
 
第二步:编译css文件
在BasicStyle.css右键选择【将CSS编译为SWF文件】
第三步:在preinitialize()事件中initApp()
	<fx:Script>
		<![CDATA[
			public function initApp():void{
				styleManager.loadStyleDeclarations("assets/BasicStyles.swf");
			}
		]]>
	</fx:Script>
	
	<s:Button label="thisisisis"/>
	<s:Label text="thank you for your letter"/>
 
知识补充
loadStyleDeclarations()方法返回的是一个IEventDispatcher类的实例,所以我们可以使用返回的对象来触发事件,我们可以使用的事件包括:

StyleEvent.PROGRESS,StyleEvent.COMPLETE, StyleEvent.ERROR

下面是一个简单的例子,虽然其并没有什么意义:【BasicLayout.swf用的依旧是上面的文件】

	<s:layout>
		<s:VerticalLayout/>
	</s:layout>
	<fx:Script>
		<![CDATA[
			import mx.events.StyleEvent;
			public function initApp():void{
				var myEvent:IEventDispatcher=styleManager.loadStyleDeclarations("assets/BasicStyles.swf");
				myEvent.addEventListener(StyleEvent.COMPLETE,getImageEventHandler);
			}
			private function getImageEventHandler(event:StyleEvent):void{
				map1.source="assets/bg.jpg";
			}
		]]>
	</fx:Script>
	<mx:Image id="map1"/>
	<s:Button label="thisisisis"/>
	<s:Label text="thank you for your letter"/>
posted @ 2010-03-23 16:20  himyspace  阅读(171)  评论(0编辑  收藏  举报