- Adobe Flex 教程
- Flex - 首頁
- Flex - 概述
- Flex - 環境
- Flex - 應用程式
- Flex - 建立應用程式
- Flex - 部署應用程式
- Flex - 生命週期階段
- Flex - 使用 CSS 樣式
- Flex - 使用皮膚樣式
- Flex - 資料繫結
- Flex - 基本控制元件
- Flex - 表單控制元件
- Flex - 複雜控制元件
- Flex - 佈局面板
- Flex - 視覺效果
- Flex - 事件處理
- Flex - 自定義控制元件
- Flex - RPC 服務
- Flex - FlexUnit 整合
- Flex - 除錯應用程式
- Flex - 國際化
- Flex - 列印支援
- Adobe Flex 資源
- Flex - 快速指南
- Flex - 有用資源
- Flex - 討論
Flex - 使用 CSS 樣式
Flex 支援使用 CSS 語法和樣式應用於其 UI 控制元件,就像 CSS 應用於 HTML 元件一樣。
方法 1:使用外部樣式表文件
您可以引用應用程式類路徑中可用的樣式表。例如,考慮 com/tutorialspoint/client 資料夾中的 Style.css 檔案,HelloWorld.mxml 檔案也位於其中。
/* 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>
方法 2:在 Ui 容器元件內使用樣式
您可以使用 <fx:Style> 標籤在 UI 容器元件內定義樣式
類選擇器
<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 選擇器
使用 id 選擇器設定 UI 元件的樣式。
<fx:Style>
/* id level selector */
#msgLabel {
color: gray;
}
</fx:Style>
<s:Label id = "msgLabel" text = "This is a normal message" />
型別選擇器
一次性設定一種 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 使用 CSS 樣式示例
讓我們按照以下步驟,透過建立一個測試應用程式來檢查 Flex 應用程式的 CSS 樣式 -
| 步驟 | 描述 |
|---|---|
| 1 | 在包 com.tutorialspoint.client 下建立一個名為 HelloWorld 的專案,如Flex - 建立應用程式章節中所述。 |
| 2 | 修改 Style.css、HelloWorld.mxml,如下所述。保持其餘檔案不變。 |
| 3 | 編譯並執行應用程式,以確保業務邏輯按要求工作。 |
以下是修改後的 CSS 檔案 src/com.tutorialspoint/Style.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;
}
以下是修改後的 mxml 檔案 src/com.tutorialspoint/HelloWorld.mxml 的內容。
<?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 - 建立應用程式 章節中一樣,以正常模式編譯並執行應用程式。如果您的應用程式一切正常,則會產生以下結果:[ 線上嘗試 ]
廣告