简体   繁体   中英

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? I've seen examples of how to do this with the MX version of the Application component, but not the spark version.

I cannot bind the backgroundColor property to a variable and modify that. However, I am thinking that I should use the styleManager property of the component to perform this change.

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

The video tutorial steps through using CSS and using skins in Flex 4 which are the primary means of changing visual components.

Application has a backgroundColor style still: http://help.adobe.com/en_US/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

<?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

<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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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