Flex - 使用 CSS 进行样式设置
Flex 支持使用 CSS 语法和样式来应用于其 UI 控件,其方式与 CSS 应用于 HTML 组件相同。
方法 # 1:使用外部样式表文件
您可以引用应用程序类路径中可用的样式表。例如,考虑 com/tutorialspoint/client 文件夹 中的 Style.css 文件,HelloWorld.mxml 文件也位于其中。
/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; ... .container { cornerRadius :10; horizontalCenter :0; borderColor: #777777; verticalCenter:0; backgroundColor: #efefef; }
然后可以通过以下代码片段引用 css 文件
<fx:Style source = "/com/tutorialspoint/client/Style.css" />
使用 styleName 属性将样式分配给 UI 组件
<s:BorderContainer width = "500" height = "500" id = "mainContainer" styleName = "container"> ... </s:BorderContainer>
方法 # 2:在 UI 容器组件中使用样式
您可以使用 <fx:Style> 标签在 UI 容器组件中定义样式
Class Level Selector
<fx:Style> @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; /* class level selector */ .errorLabel { color: red; } </fx:Style>
使用 styleName 属性为 UI 组件分配样式。
<s:Label id = "errorMsg" text = "这是一条错误消息" styleName = "errorLabel" />
Id 级别选择器
使用 id 选择器为 UI 组件设置样式。
<fx:Style> /* id level selector */ #msgLabel { color: gray; } </fx:Style> <s:Label id = "msgLabel" text = "This is a normal message" />
类型级别选择器
在一个 GO 中设置一种 UI 组件的样式。
<fx:Style> /* style applied on all buttons */ s|Button { fontSize: 15; color: #9933FF; } </fx:Style> <s:Button label = "Click Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(event)" />
带有 CSS 的 Flex 样式示例
让我们按照以下步骤通过创建测试应用程序来检查 Flex 应用程序的 CSS 样式 −
步骤 | 描述 |
---|---|
1 | 在包com.tutorialspoint.client下创建一个名为 HelloWorld 的项目,如Flex - 创建应用程序一章中所述。 |
2 | 按照以下说明修改 Style.css、HelloWorld.mxml。保持其余文件不变。 |
3 | 编译并运行应用程序以确保业务逻辑按要求运行。 |
以下是修改后的 CSS 文件 src/com.tutorialspoint/Style.css 的内容。
/* CSS file */ @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://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; }
以下是修改后的 mxml 文件 src/com.tutorialspoint/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)"> <!--Add reference to style sheet --> <fx:Style source = "/com/tutorialspoint/client/Style.css" /> <!--Using styles within mxml file --> <fx:Style> @namespace s "library://ns.adobe.com/flex/spark"; @namespace mx "library://ns.adobe.com/flex/mx"; /* class level selector */ .errorLabel { color: red; } /* id level selector */ #msgLabel { color: gray; } /* style applied on all buttons */ s|Button { fontSize: 15; color: #9933FF; } </fx:Style> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.events.FlexEvent; protected function btnClickMe_clickHandler(event:MouseEvent):void { Alert.show("Hello World!"); } protected function application_initializeHandler(event:FlexEvent):void { lblHeader.text = "CSS Demonstrating Application"; } ]]> </fx:Script> <s:BorderContainer width = "560" height = "500" id = "mainContainer" styleName = "container"> <s:VGroup width = "100%" height = "100%" gap = "50" horizontalAlign = "center" verticalAlign = "middle"> <s:Label width = "100%" id = "lblHeader" fontSize = "40" color = "0x777777" styleName = "heading" /> <s:Button label = "Click Me!" id = "btnClickMe" click = "btnClickMe_clickHandler(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:Application>
完成所有更改后,让我们像在 Flex - 创建应用程序 一章中一样,以正常模式编译并运行应用程序。如果您的应用程序一切正常,将产生以下结果:[ 在线试用 ]