重绘/刷新自定义皮肤
问题描述:
因此,我创建一个自定义切换按钮,包括财产CurrentValue的:整数重绘/刷新自定义皮肤
这样我可以执行下列操作...
<fx:Script>
protected var currentValue:int = 0;
protected function changeValue(x:int):void{
currentValue = x;
}
</fx:Script>
<MyToggleButton currentValue="{updatedValue}" styleName="MyToggleStyle" click="changeValue(1)" id="first"/>
<MyToggleButton currentValue="{updatedValue}" styleName="MyToggleStyle" click="changeValue(2)" id="second"/>
这个脚本的想法是基于currentValue中的改变而改变的切换按钮的外观。每次我如何刷新切换按钮,以便根据“currentValue”中的更改更新样式?
P.S. MyToggleStyle将引用以mxml编写的Skin类,该类将使用getStyle(“currentValue”)获取当前值的更改
答
如果currentValue
属性只会影响皮肤图形,并且您有几个预定义的“看起来“,那么你应该创建一个具有状态的皮肤。 (见spark.skins.spark.CheckBoxSkin
一个很好的例子。)
:
public function set currentValue(value:int):void {
_currentValue = value;
invalidateSkinState();
}
protected override function getCurrentSkinState():String {
return _currentValue == 9 ? "someStateName" : "otherState";
}
这会取消你的皮肤状态,每当currentValue
变化和Flex会自动调用getCurrentSkinState()
,以确定你的皮肤的新状态。然后,您可以在皮肤内使用includeIn
,条件属性等来确定其外观。
如果要在皮肤中显示currentValue
作为文字,您应该查看皮肤部位。这些基本上是您的组件定义的插槽,然后皮肤会使用诸如标签之类的组件进行“填充”。例如,标准火花按钮上的标签是外观部件labelDisplay
。该组件可以直接在其上设置文本,但实际创建它取决于皮肤。
P.S.您的示例代码中的currentValue
还需要[Bindable]
。