简体   繁体   English

Flex 4在运行时更改主应用程序的背景色

[英]Flex 4 Change Main Application Background Color at Runtime

Is it possible within Flex 4 to change the background color of an <s:Application> at runtime? 在Flex 4中是否可以在运行时更改<s:Application>的背景颜色? I've seen examples of how to do this with the MX version of the Application component, but not the spark version. 我已经看到了有关如何使用MX版本的Application组件(而不是Spark版本)执行此操作的示例。

I cannot bind the backgroundColor property to a variable and modify that. 我无法将backgroundColor属性绑定到变量并进行修改。 However, I am thinking that I should use the styleManager property of the component to perform this change. 但是,我认为我应该使用组件的styleManager属性来执行此更改。

Could anyone explain how to do this? 谁能解释如何做到这一点?

Thank you for your time. 感谢您的时间。

I recommend you go through this: 我建议您通过以下步骤:

http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7fee.html http://help.adobe.com/en_US/flex/using/WS2db454920e96a9e51e63e3d11c0bf69084-7fee.html

The video tutorial steps through using CSS and using skins in Flex 4 which are the primary means of changing visual components. 该视频教程逐步介绍了如何使用CSS和使用Flex 4中的皮肤,这些皮肤是更改视觉组件的主要方法。

Application has a backgroundColor style still: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/components/Application.html 应用程序仍然具有backgroundColor样式: http : //help.adobe.com/zh_CN/FlashPlatform/reference/actionscript/3/spark/components/Application.html

<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600"
               creationComplete="application1_creationCompleteHandler(event)">
    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
    <fx:Script>
        <![CDATA[
            import mx.events.FlexEvent;

            protected function application1_creationCompleteHandler(event:FlexEvent):void
            {
                setStyle('backgroundColor',0xCCCCCC);
            }

        ]]>
    </fx:Script>
    <s:Button click="setStyle('backgroundColor','0xff0000');" label="turn red"/>
    <s:Button click="setStyle('backgroundColor','0x0000ff');" label="turn blue"/>
    <s:Button click="setStyle('backgroundColor','0x00ff00');" label="turn green"/>
</s:Application>

A Better way to go IMO IMO的更好方法

<?xml version="1.0" encoding="utf-8"?>
<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" minWidth="955" minHeight="600">
    <s:layout>
        <s:HorizontalLayout/>
    </s:layout>
    <fx:Style>
        @namespace s "library://ns.adobe.com/flex/spark";
        @namespace mx "library://ns.adobe.com/flex/mx";
        s|Application{
            backgroundColor:#CCCCCC;
        }
    </fx:Style>
    <s:Button click="setStyle('backgroundColor','0xff0000');" label="turn red"/>
    <s:Button click="setStyle('backgroundColor','0x0000ff');" label="turn blue"/>
    <s:Button click="setStyle('backgroundColor','0x00ff00');" label="turn green"/>
</s:Application>

Better still pull the CSS out into it's own file and just reference it with a 最好还是将CSS提取到它自己的文件中,然后使用

<fx:Style source="myStyle.css"/>

You may try it with 您可以尝试

FlexGlobals.topLevelApplication.setStyle("backgroundColor", 0xff0000);  // that would turn it into bright red
FlexGlobals.topLevelApplication.setStyle("backgroundAlpha", 1);  // Sometimes background color is ignored when background alpha is zero

If the background color does not change, that means one of your component might be dictating the background color. 如果背景颜色不变,则意味着您的组件之一可能决定了背景颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM