编辑:如果您需要在运行时设置“内容”部分和“按钮”部分,仍然能够在MXML中使用您的自定义组件......好吧,我怀疑这是可能的。如果放弃MXML,则必须为组件创建一些配置对象并将其传递给构造函数,并且还需要在自定义组件中使用重写方法createChildren。 如果“按钮”部分是静态的 - 阅读下面的文本。
您可以尝试为此组件创建自定义的SkinnableComponent和皮肤类,您将在其中放置布局。
1)从SkinnableComponent延伸,这样
public class CustomSkinnable extend SkinnableComponent
{
[SkinPart[required="true"])
public var submitButton:Button;
//the same with cancel button for example
//override partAdd function to add event listeners to your buttons if you wish
//and other stuff folowing the manual
}
2)为您组分(Flash Builder中非常有用)[例如]
<s:Skin 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="{backgroundRect.width}" height="{backgroundRect.height}">
<!-- host component -->
<fx:Metadata>
[HostComponent("components.CustomSkinnable")]
</fx:Metadata>
<!-- SkinParts
name=submitButton, type=spark.components.Button, required=true
...
-->
<s:Rect id="backgroundRect"
x="0" y="0" width="100%" height="100%">
<s:stroke>
<s:SolidColorStroke color="#000000"/>
</s:stroke>
<s:fill>
<s:SolidColor color="#444444"/>
</s:fill>
</s:Rect>
<s:Group>
<!-- Content -->
<s:SkinnableContainer id="contentGroup" top="8" left="8" bottom="{buttonGroup.height + 16}" right="8" >
....content here....
</s:SkinnableContainer>
<!-- Buttons -->
<s:HGroup id="buttonGroup" left="8" bottom="8" right="8">
<s:Button id="submitButton" label="Submit"/>
... additional buttons here...
</s:HGroup>
</s:Group>
</s:Skin>
注意,ID = “contentGroup的”,而不是创建皮肤id =“content”的内容不仅仅是这样,contentGroup是放置子元素的每个皮肤的静态部分。
非常有趣的问题!您需要某种具有两个区域的模板组件,这两个区域充当某种占位符 - 占位符为内容,占位符为按钮。似乎没有皮肤或CSS的直接解决方案。嗯......让我想想! –
至少有一种方法可以让我从CSS获得边距“8”?因此,我至少可以在一个地方改变“填充”。 – HDave
有!在下面检查我的答案! –