Adobe
产品
Acrobat
Creative Cloud
创意套装
Digital Marketing Suite
Digital Publishing Suite
Elements
Photoshop
Touch Apps
更多产品
解决方案
数字营销
数字媒体
教育
金融服务业
政府部门
网页体验管理
更多解决方案
学习帮助下载公司
商店
在线商店
批量许可
查找经销商
搜索
 
信息 登录
欢迎,我的支持
我的帐户
注销
为何登录?登录后可以管理您的帐户,访问试用版下载、产品扩展和社区区域等。
Adobe
产品 分类 购买   搜索  
解決方案 公司
学习
登录 注销 我的货物 我的支持
Date Date
Qty:
Subtotal
Checkout
Adobe 开发者中心 / Flex 开发人员中心 / Flex 快速入门 /

Skinning components

by Adobe

Adobe logo

Created

22 March 2010

页面工具

在 Facebook 上共享
在 Twitter 上共享
在 LinkedIn 上共享
书签
打印

Tags

要求

用户级别

全部

必需产品

  • Flex (Download trial)

In the Flex 4 skinning model, the skin controls all visual elements of a component, including layout. The new architecture gives developers greater control over what their components look like a structured and tool-friendly way.

Spark skins can contain multiple elements, such as graphic elements, text, images, and transitions. Skins support states, so that when the state of a component changes, the skin changes as well. Skin states integrate well with transitions so that you can apply effects to one or more parts of the skins without adding much code.

You typically write Spark skin classes in MXML. You do this with MXML graphics tags (or FXG components) to draw the graphic elements, and specify child components (or subcomponents) using MXML or ActionScript.

Spark components and their skins have a contract that defines the rules that each member must follow so that they can communicate with one another. The rules for the skinning contract are as follows:

  • To have access to the host component from the skin, you must declare [HostComponent] metadata in the skin.
  • To access properties of the host component, declare the property public on the host component.
  • To use states, add a state to the skin's <s:states> block and declare [SkinState] metadata in the host component.
  • To use skin parts, use an id to specify a skin part in the skin class, and declare a public variable of the same name in the host component.

This Quick Start covers the following topics:

  • Creating a simple custom skin
  • Using skin states
  • Accessing the host component
  • Using transitions with skins

Creating a simple custom skin

When creating skins, you generally do not subclass existing skin classes. Instead, it is often easier to copy the source of an existing skin class and create another class from that.

The following example is a simplified version of the ButtonSkin class. You can create it by opening the source file for the spark.skins.spark.ButtonSkin class. You can then add and remove elements, as long as you do not violate the skinning contract.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components\SimpleButtonSkin.mxml --> <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="21" minHeight="21"> <fx:Metadata> [HostComponent("spark.components.Button")] </fx:Metadata> <!-- Specify one state for each SkinState metadata in the host component's class --> <s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states> <s:Rect left="0" right="0" top="0" bottom="0" width="69" height="20" radiusX="2" radiusY="2"> <s:stroke> <s:SolidColorStroke color="0x000000" weight="1"/> </s:stroke> </s:Rect> <s:Label id="labelDisplay" fontWeight="bold" horizontalCenter="0" verticalCenter="4" left="10" right="10" top="2" bottom="2"> </s:Label> </s:Skin>

The following application applies the SimpleButtonSkin custom skin class to one of the Button controls:

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components/SimpleButtonExample.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"> <s:HGroup> <s:Button id="myButton1" label="Default Skin"/> <s:Button id="myButton2" label="Custom Skin" skinClass="SimpleButtonSkin"/> </s:HGroup> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Using skin states

Skins can react to the state of the component. You must first define the states that the skin recognizes in a states block at the top of the skin class:

<s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states>

These states must match states that are declared with the SkinState metadata in the host component as part of the skinning contract.

In the skin class, you can then set properties based on the current state by using dot-notation syntax. For example, to set the alpha property of an element in the up state, you set the value of the alpha.up property.

The following example is a custom skin class that sets the alpha values of the label based on the state of the Button control:

Example

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components\AlphaButtonSkin.mxml --> <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="21" minHeight="21"> <fx:Metadata> [HostComponent("spark.components.Button")] </fx:Metadata> <!-- Specify one state for each SkinState metadata in the host component's class --> <s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states> <s:Rect left="0" right="0" top="0" bottom="0" width="69" height="20" radiusX="2" radiusY="2"> <s:stroke> <s:SolidColorStroke color="0x000000" weight="1"/> </s:stroke> </s:Rect> <s:Label id="labelDisplay" alpha.up="1" alpha.down=".1" alpha.over=".25" fontWeight="bold" horizontalCenter="0" verticalCenter="4" left="10" right="10" top="2" bottom="2"> </s:Label> </s:Skin>

The following application applies the AlphaButtonSkin custom skin class to one of the Button controls:

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components/AlphaButtonExample.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"> <s:HGroup> <s:Button id="myButton1" label="Default Skin"/> <s:Button id="myButton2" label="Custom Skin" skinClass="AlphaButtonSkin"/> </s:HGroup> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Accessing the host component

Spark skin classes typically specify the host component on them. The host component is the component that uses the skin. By specifying the host component, Spark skins can gain a reference to the component instance that uses the skin.

The following example is a simplified version of the ButtonSkin class. It accesses properties of the host component to set the size of the label.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components\HostComponentButtonSkin.mxml --> <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="21" minHeight="21"> <fx:Metadata> [HostComponent("spark.components.Button")] </fx:Metadata> <!-- Specify one state for each SkinState metadata in the host component's class --> <s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states> <s:Rect left="0" right="0" top="0" bottom="0" width="69" height="20" radiusX="2" radiusY="2"> <s:stroke> <s:SolidColorStroke color="0x000000" weight="1"/> </s:stroke> </s:Rect> <s:Label id="labelDisplay" fontSize="{hostComponent.getStyle('fontSize')}" fontSize.over="{hostComponent.getStyle('fontSize') + 10}" fontSize.down="{hostComponent.getStyle('fontSize') + 10}" alpha.up="1" alpha.down=".1" alpha.over=".25" fontWeight="bold" horizontalCenter="0" verticalCenter="4" left="10" right="10" top="2" bottom="2"> </s:Label> </s:Skin>

The following application applies the HostComponentButtonSkin custom skin class to one of the Button controls:

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components/HostComponentButtonExample.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"> <s:HGroup> <s:Button id="myButton1" label="Default Skin"/> <s:Button id="myButton2" label="Custom Skin" skinClass="HostComponentButtonSkin"/> </s:HGroup> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

Using transitions with skins

You can use transitions to add visual appeal to your Spark skins. Transitions are triggered off of state changes, which are explicitly supported in Spark skins. All the visuals for a transition should be defined in the skin class and not on the component.

You can use transitions in Spark skins in the same way you would use them in your application. You add a <s:transitions> tag as a child tag of the skin's root tag. You then define the transitions and which states they apply to with the toState and fromState on the <s:Transition> child tags. Because the skin is notified of state changes from the host component, you do not have to add any logic to support state changes to the skin.

The following example is a simplified version of the ButtonSkin class. It adds a transition to the font size change from the previous example.

Example

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components\TransitionButtonSkin.mxml --> <s:Skin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark" minWidth="21" minHeight="21"> <s:transitions> <s:Transition fromState="up" toState="over"> <s:Resize target="{labelDisplay}"/> </s:Transition> <s:Transition fromState="over" toState="up"> <s:Resize target="{labelDisplay}"/> </s:Transition> </s:transitions> <fx:Metadata> [HostComponent("spark.components.Button")] </fx:Metadata> <!-- Specify one state for each SkinState metadata in the host component's class --> <s:states> <s:State name="up"/> <s:State name="over"/> <s:State name="down"/> <s:State name="disabled"/> </s:states> <s:Rect id="buttonBorder" left="0" right="0" top="0" bottom="0" width="69" height="20" radiusX="2" radiusY="2"> <s:stroke> <s:SolidColorStroke color="0x000000" weight="1"/> </s:stroke> </s:Rect> <s:Label id="labelDisplay" fontSize="{hostComponent.getStyle('fontSize')}" fontSize.over="{hostComponent.getStyle('fontSize') + 10}" fontSize.down="{hostComponent.getStyle('fontSize') + 10}" alpha.up="1" alpha.down=".1" alpha.over=".25" fontWeight="bold" horizontalCenter="0" verticalCenter="4" left="10" right="10" top="2" bottom="2"> </s:Label> </s:Skin>

The following application applies the TransitionButtonSkin custom skin class to one of the Button controls:

<?xml version="1.0" encoding="utf-8"?> <!-- skinning_components/TransitionButtonExample.mxml --> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:s="library://ns.adobe.com/flex/spark"> <s:HGroup> <s:Button id="myButton1" label="Default Skin"/> <s:Button id="myButton2" label="Custom Skin" skinClass="TransitionButtonSkin"/> </s:HGroup> </s:Application>

Result

缺少 Flash player 您必须装有 Flash 10? 您必须装有 Flash 10?

Back to top

For more information

  • Spark Skinning

Back to top


This work is licensed under a Creative Commons Attribution-Noncommercial 3.0 Unported License.

产品

  • Acrobat
  • Creative Cloud
  • Creative Suite
  • Digital Marketing Suite
  • Digital Publishing Suite
  • Elements
  • 移动应用程序
  • Photoshop
  • Touch Apps

解决方案

  • 数字营销
  • 数字媒体
  • 网页体验管理

行业

  • 教育
  • 金融服务业
  • 政府部门

帮助

  • 产品帮助中心
  • 订货和退货
  • 下载和安装
  • 我的 Adobe

学习

  • Adobe 开发人员连接
  • Adobe TV
  • 培训和认证
  • 论坛
  • 设计中心

购买方式

  • 在线商店
  • 批量许可
  • 查找经销商

下载

  • Adobe Reader
  • Adobe Flash Player
  • Adobe AIR
  • Adobe Shockwave Player

公司

  • 新闻编辑室
  • 合作伙伴计划
  • 公司社会责任
  • 工作机会
  • 投资者关系
  • 事件
  • 法律
  • 安全
  • 联系 Adobe
选择您的地区 中国(更改)
选择您的地区 关闭

North America

Europe, Middle East and Africa

Asia Pacific

  • Canada - English
  • Canada - Français
  • Latinoamérica
  • México
  • United States

South America

  • Brasil
  • Africa - English
  • Österreich - Deutsch
  • Belgium - English
  • Belgique - Français
  • België - Nederlands
  • България
  • Hrvatska
  • Česká republika
  • Danmark
  • Eastern Europe - English
  • Eesti
  • Suomi
  • France
  • Deutschland
  • Magyarország
  • Ireland
  • Israel - English
  • ישראל - עברית
  • Italia
  • Latvija
  • Lietuva
  • Luxembourg - Deutsch
  • Luxembourg - English
  • Luxembourg - Français
  • الشرق الأوسط وشمال أفريقيا - اللغة العربية
  • Middle East and North Africa - English
  • Moyen-Orient et Afrique du Nord - Français
  • Nederland
  • Norge
  • Polska
  • Portugal
  • România
  • Россия
  • Srbija
  • Slovensko
  • Slovenija
  • España
  • Sverige
  • Schweiz - Deutsch
  • Suisse - Français
  • Svizzera - Italiano
  • Türkiye
  • Україна
  • United Kingdom
  • Australia
  • 中国
  • 中國香港特別行政區
  • Hong Kong S.A.R. of China
  • India - English
  • 日本
  • 한국
  • New Zealand
  • 台灣

Southeast Asia

  • Includes Indonesia, Malaysia, Philippines, Singapore, Thailand, and Vietnam - English

Copyright © 2012 Adobe Systems Incorporated. All rights reserved.

使用条款 | 隐私政策和 Cookies (更新)

京 ICP 备 10217899 号 京公网安备 110105010404