English 中文(简体)
Flex - Style with CSS
  • 时间:2024-09-17

Flex - Style with CSS


Previous Page Next Page  

Flex supports the use of CSS syntax and styles to apply to its UI controls in the same way as CSS to HTML components.

Way # 1: Using External Style Sheet File

You can refer to a style sheet available in the class path of the apppcation. For example consider Style.css file in com/tutorialspoint/cpent folder where HelloWorld.mxml file also pes.

/* CSS file */
@namespace s "pbrary://ns.adobe.com/flex/spark";
@namespace mx "pbrary://ns.adobe.com/flex/mx";
...
.container {
   cornerRadius :10;
   horizontalCenter :0;	
   borderColor: #777777;
   verticalCenter:0;
   backgroundColor: #efefef;
}

Then css file can be referred by following code snippet

<fx:Style source = "/com/tutorialspoint/cpent/Style.css" />

Assign styles to UI component using styleName property

<s:BorderContainer width = "500" height = "500" id = "mainContainer" 
   styleName = "container"> 
   ...
</s:BorderContainer>		  

Way # 2: Using Styles Within Ui Container Component

You can define styles within UI container component using <fx:Style> tag

Class Level Selector

<fx:Style>
   @namespace s "pbrary://ns.adobe.com/flex/spark";
   @namespace mx "pbrary://ns.adobe.com/flex/mx";

   /* class level selector  */
   .errorLabel {
      color: red;
   }		
</fx:Style>

Assign styles to UI component using styleName property.

<s:Label id = "errorMsg" text = "This is an error message" styleName = "errorLabel" />

Id Level Selector

Style UI component using id selector.

<fx:Style> 
   /* id level selector  */ 
   #msgLabel { 
      color: gray; 
   } 
</fx:Style>

<s:Label id = "msgLabel" text = "This is a normal message" /> 

Type Level Selector

Style one type of UI Component in one GO.

<fx:Style> 
   /* style appped on all buttons  */ 
   s|Button {  
      fontSize: 15; 
      color: #9933FF; 
   } 
</fx:Style>

<s:Button label = "Cpck Me!" id = "btnCpckMe"
   cpck = "btnCpckMe_cpckHandler(event)" />

Flex Style with CSS Example

Let us follow the steps to check CSS stypng of a Flex apppcation by creating a test apppcation −

Step Description
1 Create a project with a name HelloWorld under a packagecom.tutorialspoint.cpent as explained in the Flex - Create Apppcation chapter.
2 Modify Style.css, HelloWorld.mxml as explained below. Keep rest of the files unchanged.
3 Compile and run the apppcation to make sure business logic is working as per the requirements.

Following is the content of the modified CSS file src/com.tutorialspoint/Style.css.

/* CSS file */
@namespace s "pbrary://ns.adobe.com/flex/spark";
@namespace mx "pbrary://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;
}

Following is the content of the modified mxml file src/com.tutorialspoint/HelloWorld.mxml.

<?xml version = "1.0" encoding = "utf-8"?>
<s:Apppcation xmlns:fx = "http://ns.adobe.com/mxml/2009"
   xmlns:s = "pbrary://ns.adobe.com/flex/spark"
   xmlns:mx = "pbrary://ns.adobe.com/flex/mx"
   width = "100%" height = "100%" minWidth = "500" minHeight = "500"
   initiapze = "apppcation_initiapzeHandler(event)">
   
   <!--Add reference to style sheet -->
   <fx:Style source = "/com/tutorialspoint/cpent/Style.css" />

   <!--Using styles within mxml file -->
   <fx:Style>
      @namespace s "pbrary://ns.adobe.com/flex/spark";
      @namespace mx "pbrary://ns.adobe.com/flex/mx";

      /* class level selector  */
      .errorLabel {
         color: red;
      }

      /* id level selector  */
      #msgLabel {
         color: gray;
      }

      /* style appped on all buttons  */
      s|Button {
         fontSize: 15;
         color: #9933FF;
      }
   </fx:Style>

   <fx:Script>
      <![CDATA[
         import mx.controls.Alert;
         import mx.events.FlexEvent;
         protected function btnCpckMe_cpckHandler(event:MouseEvent):void {
            Alert.show("Hello World!");
         }

         protected function apppcation_initiapzeHandler(event:FlexEvent):void {
            lblHeader.text = "CSS Demonstrating Apppcation";
         }
      ]]>
   </fx:Script>
   
   <s:BorderContainer width = "560" height = "500" id = "mainContainer"
      styleName = "container">
      <s:VGroup width = "100%" height = "100%" gap = "50"
         horizontalApgn = "center" verticalApgn = "middle">
         <s:Label width = "100%" id = "lblHeader" fontSize = "40"
            color = "0x777777" styleName = "heading" />
         <s:Button label = "Cpck Me!" id = "btnCpckMe"
            cpck = "btnCpckMe_cpckHandler(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:Apppcation>

Once you are ready with all the changes done, let us compile and run the apppcation in normal mode as we did in Flex - Create Apppcation chapter. If everything is fine with your apppcation, this will produce following result: [ Try it onpne ]

Flex Style with CSS Advertisements