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 - 建立應用程式 章節中一樣,以正常模式編譯並執行應用程式。如果您的應用程式一切正常,則會產生以下結果:[ 線上嘗試 ]

Flex Style with CSS
廣告

© . All rights reserved.