-

Flex支持使用CSS语法和样式以与CSS到HTML组件相同的方式将样式应用于其UI控件。

方法#1:使用外部样式表文件

您可以参考应用程序的类路径中提供的样式表。 例如,将Style.css文件与HelloWorld.mxml文件拷贝到com / tutorialspoint / client文件夹。

  1. /* CSS file */
  2. @namespace s "library://ns.adobe.com/flex/spark";
  3. @namespace mx "library://ns.adobe.com/flex/mx";
  4. ...
  5. .container {
  6. cornerRadius :10;
  7. horizontalCenter :0;
  8. borderColor: #777777;
  9. verticalCenter:0;
  10. backgroundColor: #efefef;
  11. }

然后css文件可以通过下面的代码片段引用

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

使用styleName属性将样式指定给UI组件

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

方法#2:在UI容器组件中使用样式

您可以使用< fx:Style>在UI容器组件中定义样式。 标签

类级别选择器

  1. <fx:Style>
  2. @namespace s "library://ns.adobe.com/flex/spark";
  3. @namespace mx "library://ns.adobe.com/flex/mx";
  4.  
  5. /* class level selector */
  6. .errorLabel {
  7. color: red;
  8. }
  9. </fx:Style>

使用styleName属性将样式指定给UI组件。

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

Id级别选择器

使用id选择器的样式UI组件。

  1. <fx:Style>
  2. /* id level selector */
  3. #msgLabel {
  4. color: gray;
  5. }
  6. </fx:Style>
  1. <s:Label id="msgLabel" text="This is a normal message" />

类型级别选择器

在一个样式中样式一种类型的UI组件。

  1. <fx:Style>
  2. /* style applied on all buttons */
  3. s|Button {
  4. fontSize: 15;
  5. color: #9933FF;
  6. }
  7. </fx:Style>
  1. <s:Button label="Click Me!" id="btnClickMe"
  2. click="btnClickMe_clickHandler(event)" />

Flex样式与CSS示例

让我们按照以下步骤通过创建测试应用程序来检查Flex应用程序的css样式:

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

以下是修改的css文件 src / com.tutorialspoint / Style.css 的内容。

  1. /* CSS file */
  2. @namespace s "library://ns.adobe.com/flex/spark";
  3. @namespace mx "library://ns.adobe.com/flex/mx";
  4.  
  5. .heading
  6. {
  7. fontFamily: Arial, Helvetica, sans-serif;
  8. fontSize: 17px;
  9. color: #9b1204;
  10. textDecoration:none;
  11. fontWeight:normal;
  12. }
  13.  
  14. .button {
  15. fontWeight: bold;
  16. }
  17.  
  18. .container {
  19. cornerRadius :10;
  20. horizontalCenter :0;
  21. borderColor: #777777;
  22. verticalCenter:0;
  23. backgroundColor: #efefef;
  24. }

以下是修改后的mxml文件 src / com.tutorialspoint / 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. <!--Add reference to style sheet -->
  8. <fx:Style source="/com/tutorialspoint/client/Style.css"/>
  9.  
  10. <!--Using styles within mxml file -->
  11. <fx:Style>
  12. @namespace s "library://ns.adobe.com/flex/spark";
  13. @namespace mx "library://ns.adobe.com/flex/mx";
  14.  
  15. /* class level selector */
  16. .errorLabel {
  17. color: red;
  18. }
  19.  
  20. /* id level selector */
  21. #msgLabel {
  22. color: gray;
  23. }
  24.  
  25. /* style applied on all buttons */
  26. s|Button {
  27. fontSize: 15;
  28. color: #9933FF;
  29. }
  30. </fx:Style>
  31. <fx:Script>
  32. <![CDATA[
  33. import mx.controls.Alert;
  34. import mx.events.FlexEvent;
  35. protected function btnClickMe_clickHandler(event:MouseEvent)
  36. :void {
  37. Alert.show("Hello World!");
  38. }
  39.  
  40. protected function application_initializeHandler(event:FlexEvent)
  41. :void {
  42. lblHeader.text = "CSS Demonstrating Application";
  43. }
  44. ]]>
  45. </fx:Script>
  46. <s:BorderContainer width="560" height="500" id="mainContainer"
  47. styleName="container">
  48. <s:VGroup width="100%" height="100%" gap="50"
  49. horizontalAlign="center" verticalAlign="middle">
  50. <s:Label width="100%" id="lblHeader" fontSize="40"
  51. color="0x777777" styleName="heading"/>
  52. <s:Button label="Click Me!" id="btnClickMe"
  53. click="btnClickMe_clickHandler(event)" />
  54. <s:Label id="errorMsg"
  55. text="This is an error message" styleName="errorLabel" />
  56. <s:Label id="msgLabel" text="This is a normal message" />
  57. </s:VGroup>
  58. </s:BorderContainer>
  59. </s:Application>

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

Flex Style with CSS