- Adobe Flex 教程
- 伟创力 - 主页
- Flex - 概述
- Flex - 环境
- Flex - 应用
- Flex - 创建应用程序
- Flex - 部署应用程序
- Flex - 生命周期阶段
- Flex - 使用 CSS 设计样式
- Flex - 与皮肤风格
- Flex - 数据绑定
- Flex - 基本控制
- Flex - 表单控件
- Flex - 复杂的控制
- Flex - 布局面板
- Flex - 视觉效果
- Flex - 事件处理
- Flex - 自定义控件
- Flex - RPC 服务
- Flex - FlexUnit 集成
- Flex - 调试应用程序
- Flex - 国际化
- Flex - 打印支持
- Adobe Flex 资源
- Flex - 快速指南
- Flex - 有用的资源
- Flex - 讨论
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 容器组件中定义样式
班级级别选择器
<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 = "This is an error message" 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)" />
Flex 样式与 CSS 示例
让我们按照步骤通过创建一个测试应用程序来检查 Flex 应用程序的 CSS 样式 -
| 步 | 描述 |
|---|---|
| 1 | 按照Flex - 创建应用程序一章中的说明,在com.tutorialspoint.client包下创建一个名为 HelloWorld 的项目。 |
| 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 - 创建应用程序一章中所做的那样。如果您的应用程序一切正常,这将产生以下结果:[在线尝试]
