如何在 Flex 应用程序中拥有一个可以在 Flex CSS 文件中的多个位置应用的常量?例如,我可能有一个在多个 UI 组件中相同的背景颜色,然后我希望仅在一个位置设置此颜色并在所有样式子句中重用。类似...
public static const myColor = "#00FF00"
...
component1
{
backgroundColor: myColor
}
component2 {
backgroundColor: myColor
}
最佳答案
我用的就是这个。查看StylesheetMixin class on Snipplr .
这是使用时的样子:
调色板
package
{
// only make bindable if you want to use in skins
// for example like color="{ColorPalette.crazyColor}"
[Bindable]
/**
* This class holds all of your global colors to apply to skins.
*/
public class ColorPalette
{
// "var", not "const", so you can optionally change them at runtime
public static var crazyColor:uint = 0xff0000;
public static const applicationAccent:uint = 0x1a01dd;
}
}
样式表
@namespace mx "library://ns.adobe.com/flex/mx";
@namespace s "library://ns.adobe.com/flex/spark";
@namespace tlf "library://ns.adobe.com/flashx/textLayout";
mx|Panel
{
color: crazyColor;
backgroundColor: applicationAccent;
}
mx|Button
{
color: crazyColor;
backgroundColor: applicationAccent;
}
Flex 3 等效项:
Panel
{
color: crazyColor;
backgroundColor: applicationAccent;
}
Button
{
color: crazyColor;
backgroundColor: applicationAccent;
}
示例应用程序
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
xmlns:local="*">
<mx:Script>
<![CDATA[
import ColorPalette;
]]>
</mx:Script>
<!-- simple css -->
<mx:Style source="main.css"/>
<!-- stylesheet palette -->
<local:StylesheetMixin palettes="{[ColorPalette]}"/>
<!-- sample container -->
<mx:Panel id="panel" width="100%" height="100%" title="Stylesheet Palettes!">
<mx:Button label="Button"/>
</mx:Panel>
</mx:Application>
我有以下调色板:
- 颜色
- Assets
- 效果
- 布局
适用于 Flex 3 和 4。
关于css - 如何在 Flex CSS 文件中包含常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2292127/