今天发现,如果但用AS3的话,是不能通过更换CSS来给程序换肤的。
- Test One-----MXML下更换CSS
TestInFlex.mxml--↓--
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ public function loadStyle():void { StyleManager.loadStyleDeclarations("Theme/style.swf"); } public function unloadStyle():void { StyleManager.unloadStyleDeclarations("Theme/style.swf"); } ]]> </mx:Script> <mx:Button id="buttonLoadStyle" x="259" y="90" click="loadStyle();" label="LoadStyle"/> <mx:Button x="369" y="90" click="unloadStyle();" label="UnloadStyle"/> <mx:Button x="259" y="37" label="Button"/> </mx:Application>
Style.css--↓--
/* CSS file */ Button { fontSize: 16; }
编译之后点击按钮可以更换样式。
- Test Two-----纯AS下更换CSS
代码不贴了。原因是根本行不通的说。纯AS项目,没有Flex的组件,所以也没有StyleManager类。而如果硬性在纯AS项目的Library Path中添加Flex组件--swc文件的话,会导致项目不进行编译。不知道是什么问题。
- Test Three-----AIR项目中MXML下更换CSS
TestInAir.mxml
<?xml version="1.0" encoding="utf-8"?> <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"> <mx:Script> <![CDATA[ import mx.styles.StyleManager; private function loadIt():void{ StyleManager.loadStyleDeclarations("Theme/style.swf"); } ]]> </mx:Script> <mx:Button label="测试" click="loadIt()"/> </mx:WindowedApplication>
同样的样式文件。这次能成功更换样式。
- Test Four-----AIR项目中AS下更换CSS
TextInAir.as
package
{
import flash.display.NativeWindow;
import flash.display.NativeWindowInitOptions;
import flash.display.NativeWindowType;
import flash.display.SimpleButton;
import flash.display.Sprite;
import flash.display.StageScaleMode;
import mx.styles.StyleManager;
public class FlexFinderMain extends Sprite
{
public function FlexFinderMain()
{
super();
var options:NativeWindowInitOptions = new NativeWindowInitOptions();
options.type = NativeWindowType.UTILITY;
var window:NativeWindow = new NativeWindow(options);
window.stage.scaleMode = StageScaleMode.NO_SCALE;
var myButton:SimpleButton = new SimpleButton();
window.stage.addChild(myButton);
StyleManager.loadStyleDeclarations("Theme/style.swf");
window.width = 200;
window.height = 200;
window.activate();
}
}
}
运行的时候就会出错:
Error: No class registered for interface 'mx.styles::IStyleManager2'. at mx.core::Singleton$/getInstance()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\core\Singleton.as:111] at mx.styles::StyleManager$/get impl()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManager.as:78] at mx.styles::StyleManager$/loadStyleDeclarations()[C:\autobuild\3.2.0\frameworks\projects\framework\src\mx\styles\StyleManager.as:555] at FlexFinderMain()[E:\Folder-Program\Eclipse\Workspace\FlexFinder\src\FlexFinderMain.as:27] at runtime::ContentPlayer/loadInitialContent() at runtime::ContentPlayer/playRawContent() at runtime::ContentPlayer/playContent() at runtime::AppRunner/run() at global/runtime::ADLEntry()
当然如果去掉StyleManager.loadStyleDeclarations("Theme/style.swf");就不会有错。
So.....