课程表

Flex课程

工具箱
速查手册

Flex Style与Skin

当前位置:免费教程 » 软件/图像 » Flex

什么是Skinning?

  • Flex中的Skinning是一个完全自定义UI组件的外观和感觉的过程。

  • 皮肤可以定义组件的文本,图像,过滤器,过渡和状态。

  • 可以将皮肤创建为单独的mxml和ActionScript组件。

  • 使用皮肤,我们可以控制UI组件的所有视觉方面。

  • 定义皮肤的过程对于所有UI组件是相同的。

步骤1:创建皮肤

使用选项File > New > MXML Skin 创建MXML皮肤向导

Flex Skin Wizard

输入包为 com.tutorialspoint.skin 的包,名称为 GradientBackgroundSkin ,并选择主机组件作为现有flex BorderContainer控件 spark.component.BorderContainer

现在你已经为BorderContainer创建了一个外观。 修改mxml皮肤文件 src / com.tutorialspoint / skin / GradientBackgroundSkin.mxml 的内容。 更新填充图层如下:

  1. <!-- fill -->
  2. <s:Rect id="backgroundRect" left="0" right="0" height="100%" top="0">
  3. <s:fill>
  4. <s:LinearGradient rotation="90">
  5. <s:GradientEntry color="0x888888" ratio="0.2"/>
  6. <s:GradientEntry color="0x111111" ratio="1"/>
  7. </s:LinearGradient>
  8. </s:fill>
  9. </s:Rect>

步骤2:涂抹皮肤

您可以使用两种方式在组件上应用皮肤

在MXML脚本中应用皮肤(静态)

使用 skinClass 属性将 GradientBackgroundSkin 应用于ID为 mainContainer 的BorderContainer。

  1. <s:BorderContainer width="560" height="500" id="mainContainer"
  2. styleName="container">
  3. <s:VGroup width="100%" height="100%" gap="50"
  4. horizontalAlign="center" verticalAlign="middle"
  5. skinClass="com.tutorialspoint.skin.GradientBackgroundSkin">

在ActionScript中应用皮肤(动态)

使用 skinClass 属性将 GradientBackgroundSkin 应用于ID为 mainContainer 的BorderContainer。

  1. protected function gradientBackground_clickHandler(event:MouseEvent):void
  2. {
  3. mainContainer.setStyle("skinClass", GradientBackgroundSkin );
  4. }

Flex样式与皮肤示例

让我们按照以下步骤在Flex应用程序中通过创建测试应用程序来查看操作中的换肤:

描述
1 Flex - 创建应用程序章节中所述,在包 com.tutorialspoint.client 下创建名为 HelloWorld 的项目。
2在如上所述的软件包 com.tutorialspoint.skin 下创建外观 GradientBackgroundSkin.mxml 保持文件的其余部分不变。
3修改 HelloWorld.mxml ,如下所述。 保持文件的其余部分不变。
4编译并运行应用程序,以确保业务逻辑按照要求工作。

以下是 GradientBackgroundSkin.mxml 文件 src / com / tutorialspoint / skin / GradientBackgroundSkin.mxml 的内容。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx">
  5. <!-- host component -->
  6. <fx:Metadata>
  7. [HostComponent("spark.components.BorderContainer")]
  8. </fx:Metadata>
  9.  
  10. <!-- states -->
  11. <s:states>
  12. <s:State name="disabled" />
  13. <s:State name="disabled" />
  14. <s:State name="normal" />
  15. </s:states>
  16.  
  17. <!-- SkinParts
  18. name=contentGroup, type=spark.components.Group, required=false
  19. -->
  20. <!-- fill -->
  21. <s:Rect id="backgroundRect" left="0" right="0" height="100%" top="0">
  22. <s:fill>
  23. <s:LinearGradient rotation="90">
  24. <s:GradientEntry color="0x111111" ratio="0.2"/>
  25. <s:GradientEntry color="0x888888" ratio="1"/>
  26. </s:LinearGradient>
  27. </s:fill>
  28. </s:Rect>
  29. <!-- must specify this for the host component -->
  30. <s:Group id="contentGroup" left="0" right="0" top="0" bottom="0" />
  31. </s:Skin>

以下是修改的 HelloWorld.mxml 文件 src / com / tutorialspoint / client / HelloWorld.mxml 的内容。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
  3. xmlns:s="library://ns.adobe.com/flex/spark"
  4. xmlns:mx="library://ns.adobe.com/flex/mx"
  5. width="100%" height="100%" minWidth="500" minHeight="500"
  6. initialize="application_initializeHandler(event)">
  7.  
  8. <fx:Style source="/com/tutorialspoint/client/Style.css"/>
  9.  
  10. <fx:Script>
  11. <![CDATA[
  12. import com.tutorialspoint.skin.GradientBackgroundSkin;
  13. import mx.controls.Alert;
  14. import mx.events.FlexEvent;
  15. import spark.skins.spark.BorderContainerSkin;
  16.  
  17. protected function btnClickMe_clickHandler(event:MouseEvent):void
  18. {
  19. Alert.show("Hello World!");
  20. }
  21.  
  22. protected function application_initializeHandler(event:FlexEvent):void
  23. {
  24. lblHeader.text = "My Hello World Application";
  25. }
  26.  
  27. protected function gradientBackground_clickHandler(event:MouseEvent):void
  28. {
  29. mainContainer.setStyle("skinClass", GradientBackgroundSkin );
  30. }
  31.  
  32. protected function standardBackground_clickHandler(event:MouseEvent):void
  33. {
  34. mainContainer.setStyle("skinClass", BorderContainerSkin );
  35. }
  36. ]]>
  37. </fx:Script>
  38. <fx:Declarations>
  39. <s:RadioButtonGroup id="selectorGroup" />
  40. </fx:Declarations>
  41. <s:BorderContainer width="500" height="500" id="mainContainer"
  42. skinClass="spark.skins.spark.BorderContainerSkin"
  43. horizontalCenter="0" verticalCenter="0" cornerRadius="10">
  44. <s:VGroup width="100%" height="100%" gap="50" horizontalAlign="center"
  45. verticalAlign="middle">
  46. <s:Label id="lblHeader" fontSize="40" color="green"
  47. styleName="heading"/>
  48. <s:Button label="Click Me!" id="btnClickMe"
  49. click="btnClickMe_clickHandler(event)"/>
  50. <s:RadioButton color="gray" fontWeight="bold"
  51. group="{selectorGroup}" label="Standard Background"
  52. click="standardBackground_clickHandler(event)" selected="true"/>
  53. <s:RadioButton color="gray" fontWeight="bold"
  54. group="{selectorGroup}" label="Gradient Background"
  55. click="gradientBackground_clickHandler(event)"/>
  56. </s:VGroup>
  57. </s:BorderContainer>
  58. </s:Application>

准备好所有更改后,让我们以正常模式编译和运行应用程序,就像在 Flex - 创建应用程序中一样 章节。
  如果一切顺利,您的应用程序,这将产生以下结果:[在线试用]

Flex Skin Style1Flex Skin Style2
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号