使用Flex 3 textRollOverColor
和textSelectedColor
但Flex 4組件不再支持它們。
下面的例子演示了所有該顏色的火花名單這個+添加支持:
<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">
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
global
{
textRollOverColor: yellow;
textSelectedColor: green;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
private function getListDataProvider():ArrayCollection
{
return new ArrayCollection([ "Item 1", "Item 2", "Item 3"]);
}
private function getGridDataProvider():ArrayCollection
{
return new ArrayCollection([ { name: "Item 1" }, { name: "Item 2" }, { name: "Item 3" } ]);
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout horizontalAlign="center" verticalAlign="middle"/>
</s:layout>
<s:List dataProvider="{getListDataProvider()}"/>
<s:List dataProvider="{getListDataProvider()}" itemRenderer="ColoredItemRenderer"/>
<mx:List dataProvider="{getListDataProvider()}"/>
<mx:DataGrid dataProvider="{getGridDataProvider()}"/>
</s:Application>
ColoredItemRenderer
:
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true">
<fx:Script>
<![CDATA[
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
var color:uint;
if (selected)
color = getStyle("textSelectedColor");
else if (hovered)
color = getStyle("textRollOverColor");
else
color = getStyle("color");
sparkLabel.setStyle("color", color);
}
]]>
</fx:Script>
<s:Label id="sparkLabel" text="{data}"/>
</s:ItemRenderer>
我用你的代碼來幫助我解決SAP Dashboards SDK中一個蹩腳的錯誤(Overridebase錯誤),當嘗試使用帶有狀態的Spark標籤的自定義ItemRenderer時 - 覆蓋updateDisplayList就像你的方法一樣完美! – 2014-02-19 12:49:17
這不適用於Spark DataGrid,因爲updateDisplayList方法只調用一次。此外,它會引發錯誤,因爲它需要成爲IGridItemRenderer,以便從GridItemRenderer擴展。我爲下面的DataGrid添加了一個答案。 – 2016-02-07 07:20:56