FLASH MX2004体验二:定制组件外观
作者: 来源: 添加时间:2006-5-23 7:54:24改变组件外观有三种方法: Use the Styles API(使用样式API), Apply a theme(应用一个主题名), 和Modify or replace a component's skins(修改或替换组皮肤). 这里着重说明第一种方法, Use the Styles API(application programming interface). 我们可以利用Styles API 提供的属性和方法( setStyle()和getStyle())来改变组件的颜色和文本格式.
其中使用Styles又有四种方法:
1.Set styles on a component instance. (对一个组件实例设置样式)
2.Use the _global style declaration that sets styles for all components in a document.(使用全局样式声明对文档的所有组件设置样式)
3.Create custom style declarations and apply them to specific component instances.(创建自定义样式声明并应用到指定的组件实例)
4.Create default class style declarations.(为组件类别创建样式声明)
下面对每一种方法进行详解.
Set styles on a component instance (对一个组件实例设置样式).
从components面板拖个Button 组件到场景(也可以用其它组件),并命名为”myBtn”:

快捷键”F9”打开Action面板,在时间线上写Action:
|
Ctrl+Enter 预览效果,组件样式是不是改变了?下面是默认的样式和刚才自定义样式的效果对比图:

Use the _global style declaration that sets styles for all components in a document.(使用全局样式声明对文档的所有组件设置样式)
FlashMX2004的_global对象的Style属性可以做到这一点: 从components面板拖n 个组件到场景,这里我用的是Button,CheckBox和RadioButton 三个组件,也不要给组件命名,直接在时间线上写Action:
|
Ctrl+Enter 预览效果.下面是这三个组件的前后效果对比图:

这种方法使得场景里的所有组件的外观保持一致.
Create custom style declarations and apply them to specific component instances.(创建自定义样式声明并应用到指定的组件实例)
当我们把组件拖到场景里便可以利用CSSStyleDeclaration 对象创建一个样式声明来改变指定组件的外观。还是从components面板拖Button,CheckBox和RadioButton三个组件到场景并分别命名为CompA, CompB, CompC. 在时间线上写Action:
|
下图为三个组件应用样式后的效果:

Create default class style declarations.(为组件类别创建样式声明)
这种方法为场景内某一类型的组件定义样式,只对此类别有效。先写Action:
|
然后分别拖3个(个数根据需要而定)Button组件,CheckBox组件,RadioButton组件到场景。
预览效果如下图:

用Styles API 定义组件外观的四种方法介绍完了,呵呵,是不是很象用CSS定义HTML表单对象的样式?
[注:以上例子只对Flash V2 Component有效]
通过学习用CSS格式化Flash文本和用样式声明改变组件外观,大家可能感觉到MM正在用Flash打造一个和HTML一样的平台,呵呵,拭目以待~!