Flex支持使用CSS語法和樣式來應用於其UI控制項,就像CSS應用於HTML組件一樣。
Way # 1: Using External Style Sheet File
/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; ... .container { cornerRadius :10; horizontalCenter :0; borderColor: #777777; verticalCenter:0; backgroundColor: #efefef; }
然後可以通過以下代碼片段引用css文件
<fx:Style source = "/com/tutorialspoint/client/Style.css" />
使用styleName屬性將樣式分配給UI組件
<s:BorderContainer width = "500" height = "500" id = "mainContainer" styleName = "container"> ... </s:BorderContainer>
Way # 2: Using Styles Within Ui Container Component
可以使用<fx:Style>標記在UI容器組件中定義樣式
Class Level Selector
<fx:Style> @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; /* class level selector */ .errorLabel { color: red; } </fx:Style>
使用styleName屬性將樣式分配給UI組件.
<s:Label id = "errorMsg" text = "This is an error message" styleName = "errorLabel" />
Id Level Selector
使用id選擇器設置UI組件的樣式。
<fx:Style> /* id level selector */ #msgLabel { color: gray; } </fx:Style> <s:Label id = "msgLabel" text = "This is a normal message" />
Type Level Selector
一次性設置一種UI組件的樣式。
<fx:Style> /* style applied on all buttons */ s|Button { fontSize: 15; color: #9933FF; } </fx:Style> <s:Button label = "Click Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(event)" />
Flex Style with CSS Example
讓我們按照以下步驟創建一個測試應用程式來檢查Flex應用程式的CSS樣式;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a packagecom.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Modify Style.css, HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
下面是修改後的CSS文件的內容。
/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; .heading { fontFamily: Arial, Helvetica, sans-serif; fontSize: 17px; color: #9b1204; textDecoration:none; fontWeight:normal; } .button { fontWeight: bold; } .container { cornerRadius :10; horizontalCenter :0; borderColor: #777777; verticalCenter:0; backgroundColor: #efefef; }
<?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/mx" width = "100%" height = "100%" minWidth = "500" minHeight = "500" initialize = "application_initializeHandler(event)"> <!--Add reference to style sheet --> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <!--Using styles within mxml file --> <fx:Style> @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; /* class level selector */ .errorLabel { color: red; } /* id level selector */ #msgLabel { color: gray; } /* style applied on all buttons */ s|Button { fontSize: 15; color: #9933FF; } </fx:Style> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; protected function btnClickMe_clickHandler(event:MouseEvent):void { Alert.show("Hello World!"); } protected function application_initializeHandler(event:FlexEvent):void { lblHeader.text = "CSS Demonstrating Application"; } ]]> </fx:Script> <s:BorderContainer width = "560" height = "500" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center" verticalAlign = "middle"> <s:Label width = "100%" id = "lblHeader" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Button label = "Click Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(event)" /> <s:Label id = "errorMsg" text = "This is an error message" styleName = "errorLabel" /> <s:Label id = "msgLabel" text = "This is a normal message" /> </s:VGroup> </s:BorderContainer> </s:Application>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,這將產生以下結果:[聯機試用]