Flex中如何扩展LinkButton控件并且添加自定义的样式的例子
By Minidxer | September 9, 2008
接下来的例子演示了Flex中如何扩展LinkButton控件并且添加自定义的样式。
让我们先来看一下Demo(可以右键View Source或点击这里察看源代码):
下面是完整代码(或点击这里察看):
Download: main.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application name="LinkButton_skin_test"
- xmlns:mx="http://www.adobe.com/2006/mxml"
- xmlns:comps="comps.*"
- layout="vertical"
- verticalAlign="middle"
- backgroundColor="white">
- <mx:ApplicationControlBar dock="true">
- <mx:Form styleName="plain">
- <mx:FormItem label="enabled:">
- <mx:CheckBox id="checkBox" selected="true" />
- </mx:FormItem>
- </mx:Form>
- </mx:ApplicationControlBar>
- <comps:CustomLinkButton1 id="linkButtonMXML"
- label="LinkButton (MXML)"
- toggle="true"
- enabled="{checkBox.selected}"
- skin="skins.CustomLinkButtonSkin1"
- rollOverColor="red"
- selectionColor="haloOrange"
- toggleBackgroundColor="yellow" />
- <comps:CustomLinkButton2 id="linkButtonAS"
- label="LinkButton (ActionScript)"
- toggle="true"
- enabled="{checkBox.selected}"
- skin="skins.CustomLinkButtonSkin1"
- rollOverColor="red"
- selectionColor="haloOrange"
- toggleBackgroundColor="yellow" />
- </mx:Application>
下面是CustomLinkButton1.mxml的代码:
Download: CustomLinkButton1.mxml
- <?xml version="1.0" encoding="utf-8"?>
- <mx:LinkButton xmlns:mx="http://www.adobe.com/2006/mxml">
- <mx:Metadata>
- [Style(name="toggleBackgroundColor",
- type="uint",
- format="Color",
- inherit="yes")]
- </mx:Metadata>
- <mx:Script>
- <![CDATA[
- override public function set enabled(value:Boolean):void {
- super.enabled = value;
- useHandCursor = value;
- }
- ]]>
- </mx:Script>
- </mx:LinkButton>
下面是CustomLinkButton2.as的代码:
Download: CustomLinkButton2.as
- package comps {
- import mx.controls.LinkButton;
- [Style(name="toggleBackgroundColor",
- type="uint",
- format="Color",
- inherit="yes")]
- public class CustomLinkButton2 extends LinkButton {
- public function CustomLinkButton2() {
- super();
- }
- override public function set enabled(value:Boolean):void {
- super.enabled = value;
- useHandCursor = value;
- }
- }
- }
下面是CustomLinkButtonSkin1.as的代码:
Download: CustomLinkButtonSkin1.as
- package skins {
- import mx.skins.halo.LinkButtonSkin;
- import mx.styles.StyleManager;
- public class CustomLinkButtonSkin1 extends LinkButtonSkin {
- public function CustomLinkButtonSkin1() {
- super();
- }
- override protected function updateDisplayList(w:Number, h:Number):void {
- super.updateDisplayList(w, h);
- // Inherited styles
- var cornerRadius:Number = getStyle("cornerRadius");
- var rollOverColor:uint = getStyle("rollOverColor");
- var selectionColor:uint = getStyle("selectionColor");
- // Custom styles
- var toggleBackgroundColor:uint = getStyle("toggleBackgroundColor") || getStyle("themeColor");
- graphics.clear();
- switch (name) {
- case "upSkin":
- // Draw invisible shape so we have a hit area.
- drawRoundRect(
- 0, /* x */
- 0, /* y */
- w, /* width */
- h, /* height */
- cornerRadius, /* cornerRadius */
- 0, /* color */
- 0.0 /* alpha */
- );
- break;
- case "selectedUpSkin":
- case "selectedOverSkin":
- drawRoundRect(0, 0, w, h, cornerRadius, toggleBackgroundColor, 1.0);
- break;
- case "overSkin":
- drawRoundRect(0, 0, w, h, cornerRadius, rollOverColor, 1.0);
- break;
- case "selectedDownSkin":
- case "downSkin":
- drawRoundRect(0, 0, w, h, cornerRadius, selectionColor, 1.0);
- break;
- case "selectedDisabledSkin":
- // Draw 20% alpha shape so we have a hit area.
- drawRoundRect(0, 0, w, h, cornerRadius, toggleBackgroundColor, 0.2);
- break;
- case "disabledSkin":
- // Draw invisible shape so we have a hit area.
- drawRoundRect( 0, 0, w, h, cornerRadius, 0, 0.0);
- break;
- }
- }
- }
- }
代码:Peter deHaan 翻译/整理/编译:中文Flex例子
Topics:
Flex |
No Comments » |
752 views
Tags: LinkButton, Skin, updateDisplayList