Flex-style-with-css
フレックス-CSSを使用したスタイル
Flexは、CSS構文とスタイルの使用をサポートして、CSS to HTMLコンポーネントと同じ方法でUIコントロールに適用します。
方法#1:外部スタイルシートファイルを使用する
アプリケーションのクラスパスで利用可能なスタイルシートを参照できます。 たとえば、HelloWorld.mxmlファイルもある com/finddevguides/client folder にあるStyle.cssファイルを考えます。
/*CSS file*/
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
...
.container {
cornerRadius :10;
horizontalCenter :0;
borderColor: #777777;
verticalCenter:0;
backgroundColor: #efefef;
}
その後、CSSファイルは次のコードスニペットで参照できます
<fx:Style source = "/com/finddevguides/client/Style.css"/>
styleNameプロパティを使用して、UIコンポーネントにスタイルを割り当てます
<s:BorderContainer width = "500" height = "500" id = "mainContainer"
styleName = "container">
...
</s:BorderContainer>
方法2:Ui Containerコンポーネント内でスタイルを使用する
<fx:Style>タグを使用して、UIコンテナコンポーネント内でスタイルを定義できます
クラスレベルセレクター
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
/*class level selector */
.errorLabel {
color: red;
}
</fx:Style>
styleNameプロパティを使用して、UIコンポーネントにスタイルを割り当てます。
<s:Label id = "errorMsg" text = "This is an error message" styleName = "errorLabel"/>
Idレベルセレクター
IDセレクターを使用したスタイルUIコンポーネント。
<fx:Style>
/*id level selector */
#msgLabel {
color: gray;
}
</fx:Style>
<s:Label id = "msgLabel" text = "This is a normal message"/>
タイプレベルセレクター
1つのGOで1つのタイプのUIコンポーネントをスタイルします。
<fx:Style>
/*style applied on all buttons */
s|Button {
fontSize: 15;
color: #9933FF;
}
</fx:Style>
<s:Button label = "Click Me!" id = "btnClickMe"
click = "btnClickMe_clickHandler(event)"/>
CSSを使用したFlexスタイルの例
テストアプリケーションを作成して、FlexアプリケーションのCSSスタイリングを確認する手順に従います-
Step | Description |
---|---|
1 | Create a project with a name HelloWorld under a packagecom.finddevguides.client as explained in the Flex - Create Application chapter. |
2 | Modify Style.css, HelloWorld.mxml as explained below. Keep rest of the files unchanged. |
3 | Compile and run the application to make sure business logic is working as per the requirements. |
以下は、変更されたCSSファイル src/com.finddevguides/Style.css の内容です。
/*CSS file*/
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
.heading
{
fontFamily: Arial, Helvetica, sans-serif;
fontSize: 17px;
color: #9b1204;
textDecoration:none;
fontWeight:normal;
}
.button {
fontWeight: bold;
}
.container {
cornerRadius :10;
horizontalCenter :0;
borderColor: #777777;
verticalCenter:0;
backgroundColor: #efefef;
}
以下は、変更されたmxmlファイル src/com.finddevguides/HelloWorld.mxml の内容です。
<?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"
width = "100%" height = "100%" minWidth = "500" minHeight = "500"
initialize = "application_initializeHandler(event)">
<!--Add reference to style sheet -->
<fx:Style source = "/com/finddevguides/client/Style.css"/>
<!--Using styles within mxml file -->
<fx:Style>
@namespace s "library://ns.adobe.com/flex/spark";
@namespace mx "library://ns.adobe.com/flex/mx";
/*class level selector */
.errorLabel {
color: red;
}
/*id level selector */
#msgLabel {
color: gray;
}
/*style applied on all buttons */
s|Button {
fontSize: 15;
color: #9933FF;
}
</fx:Style>
<fx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.events.FlexEvent;
protected function btnClickMe_clickHandler(event:MouseEvent):void {
Alert.show("Hello World!");
}
protected function application_initializeHandler(event:FlexEvent):void {
lblHeader.text = "CSS Demonstrating Application";
}
]]>
</fx:Script>
<s:BorderContainer width = "560" height = "500" id = "mainContainer"
styleName = "container">
<s:VGroup width = "100%" height = "100%" gap = "50"
horizontalAlign = "center" verticalAlign = "middle">
<s:Label width = "100%" id = "lblHeader" fontSize = "40"
color = "0x777777" styleName = "heading"/>
<s:Button label = "Click Me!" id = "btnClickMe"
click = "btnClickMe_clickHandler(event)" />
<s:Label id = "errorMsg"
text = "This is an error message" styleName = "errorLabel"/>
<s:Label id = "msgLabel" text = "This is a normal message"/>
</s:VGroup>
</s:BorderContainer>
</s:Application>
すべての変更が完了したら、link:/flex/flex_create_application [Flex-アプリケーションの作成]の章で行ったように、アプリケーションを通常モードでコンパイルして実行します。 アプリケーションに問題がなければ、次の結果が得られます。[link:/flex/samples/CSSApplicationl [オンラインで試す]]