May 25, 2021 Flex
Skinning in Flex is a process of fully customizing the look and feel of UI components.
Skin can define component text, images, filters, transitions and states.
You can create skins as separate mxml and ActionScript components.
With skin, we can control all visual aspects of UI components.
The process of defining skin is the same for all UI components.
Create
an
MXML skin wizard with
the option File and New and MXML Skin
Enter the package for com.tutorialspoint.skin, named GradientBackgroundSkin, and select the host component as the existing flex BorderContainer control spark.component.BorderContainer.
Now you've created a look for BorderContainer. /b10> Modify the contents of mxml skin file src / com.tutorialspoint / skin / GradientBackgroundSkin.mxml. The update fill layer is as follows:
<!-- fill --> <s:Rect id="backgroundRect" left="0" right="0" height="100%" top="0"> <s:fill> <s:LinearGradient rotation="90"> <s:GradientEntry color="0x888888" ratio="0.2"/> <s:GradientEntry color="0x111111" ratio="1"/> </s:LinearGradient> </s:fill> </s:Rect>
You can apply skin to components in two ways
Use the skinClass property to apply GradientBackgroundSkin to BorderContainer with an ID of mainContainer.
<s:BorderContainer width="560" height="500" id="mainContainer" styleName="container"> <s:VGroup width="100%" height="100%" gap="50" horizontalAlign="center" verticalAlign="middle" skinClass="com.tutorialspoint.skin.GradientBackgroundSkin">
Use the skinClass property to apply GradientBackgroundSkin to BorderContainer with an ID of mainContainer.
protected function gradientBackground_clickHandler(event:MouseEvent):void { mainContainer.setStyle("skinClass", GradientBackgroundSkin ); }
Let's follow these steps in the Flex app to see skin changes in action by creating a test application:
Step | Describe |
---|---|
1 | As described in the Flex - Create Applications section, create a project called HelloWorld under package com.tutorialspoint.client. |
2 | Create the look and feel of GradientBackgroundSkin.mxml under the package com.tutorialspoint.skin described above. /b10> Keep the rest of the file unchanged. |
3 | Modify HelloWorld.mxml, as described below. /b10> Keep the rest of the file unchanged. |
4 | Compile and run the application to ensure that the business logic works as required. |
Here's what's in the GradientBackgroundSkin.mxml file src / com / tutorialspoint / skin / GradientBackgroundSkin.mxml.
<?xml version="1.0" encoding="utf-8"?> <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx"> <!-- host component --> <fx:Metadata> [HostComponent("spark.components.BorderContainer")] </fx:Metadata> <!-- states --> <s:states> <s:State name="disabled" /> <s:State name="disabled" /> <s:State name="normal" /> </s:states> <!-- SkinParts name=contentGroup, type=spark.components.Group, required=false --> <!-- fill --> <s:Rect id="backgroundRect" left="0" right="0" height="100%" top="0"> <s:fill> <s:LinearGradient rotation="90"> <s:GradientEntry color="0x111111" ratio="0.2"/> <s:GradientEntry color="0x888888" ratio="1"/> </s:LinearGradient> </s:fill> </s:Rect> <!-- must specify this for the host component --> <s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" /> </s:Skin>
The following is the contents of the modified HelloWorld.mxml file src / com / tutorialspoint / client / 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)"> <fx:Style source="/com/tutorialspoint/client/Style.css"/> <fx:Script> <![CDATA[ import com.tutorialspoint.skin.GradientBackgroundSkin; import mx.controls.Alert; import mx.events.FlexEvent; import spark.skins.spark.BorderContainerSkin; protected function btnClickMe_clickHandler(event:MouseEvent):void { Alert.show("Hello World!"); } protected function application_initializeHandler(event:FlexEvent):void { lblHeader.text = "My Hello World Application"; } protected function gradientBackground_clickHandler(event:MouseEvent):void { mainContainer.setStyle("skinClass", GradientBackgroundSkin ); } protected function standardBackground_clickHandler(event:MouseEvent):void { mainContainer.setStyle("skinClass", BorderContainerSkin ); } ]]> </fx:Script> <fx:Declarations> <s:RadioButtonGroup id="selectorGroup" /> </fx:Declarations> <s:BorderContainer width="500" height="500" id="mainContainer" skinClass="spark.skins.spark.BorderContainerSkin" horizontalCenter="0" verticalCenter="0" cornerRadius="10"> <s:VGroup width="100%" height="100%" gap="50" horizontalAlign="center" verticalAlign="middle"> <s:Label id="lblHeader" fontSize="40" color="green" styleName="heading"/> <s:Button label="Click Me!" id="btnClickMe" click="btnClickMe_clickHandler(event)"/> <s:RadioButton color="gray" fontWeight="bold" group="{selectorGroup}" label="Standard Background" click="standardBackground_clickHandler(event)" selected="true"/> <s:RadioButton color="gray" fontWeight="bold" group="{selectorGroup}" label="Gradient Background" click="gradientBackground_clickHandler(event)"/> </s:VGroup> </s:BorderContainer> </s:Application>
When all the changes are ready, let's compile and run the application in
normal mode, as in the Flex -
Create Application
chapter.
If all goes well with your application, this will result in the following results:
]