在Flex中,蒙皮是一個完全定製UI組件外觀的過程。
皮膚可以定義組件的文本、圖像、過濾器、轉換和狀態。
可以將皮膚創建爲單獨的mxml或ActionScript組件。
使用皮膚,我們可以控制UI組件的所有視覺方面。
對於所有UI組件,定義皮膚的過程都是相同的。
Step 1 – Create a Skin
使用「新建MXML外觀」選項啓動「創建MXML外觀」嚮導。
更新填充層,如下所示−
<!-- 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>
Step 2: Apply Skin
你可以用兩種方法在一個組件上塗抹皮膚;
Apply skin in MXML script (statically)
使用其skinClass屬性將GradientBackgroundSkin應用於idmainContainer的邊界容器。
<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">
Apply skin in ActionScript (dynamically)
使用其skinClass屬性將GradientBackgroundSkin應用於idmainContainer的邊界容器。
protected function gradientBackground_clickHandler(event:MouseEvent):void { mainContainer.setStyle("skinClass", GradientBackgroundSkin); }
Flex Style with Skin Example
讓我們按照以下步驟,通過創建一個測試應用程式來查看Flex應用程式中的蒙皮操作;
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a packagecom.tutorialspoint.client as explained in the Flex - Create Application chapter. |
2 | Create skin GradientBackgroundSkin.mxml under a packagecom.tutorialspoint.skin as explained above. Keep rest of the files unchanged. |
3 | Modify HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
4 | Compile and run the application to make sure business logic is working as per the requirements. |
<?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>
<?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>
完成所有更改後,讓我們以正常模式編譯並運行應用程式,就像我們在flex-create application一章中所做的那樣。如果您的應用程式一切正常,它將產生以下結果:[聯機試用]