Aligning labels in a Flex PopUpButton control’s pop up menu

本文介绍如何通过设置popUpStyleName和textAlign样式,在Flex框架中调整PopupButton控制的弹出菜单内标签的对齐方式。提供两种示例代码,一种为静态配置,另一种展示了动态调整菜单项文本对齐的方法。
The following example shows you how you can align the labels in a PopUpButton control’s pop up menu in Flex by setting the popUpStyleName and textAlign styles
<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2008/01/18/aligning-labels-in-a-flex-popupbutton-controls-pop-up-menu/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout
="vertical"
        verticalAlign
="top"
        backgroundColor
="white">

    
<mx:Script>
        
<![CDATA[
            import mx.controls.Menu;
            import mx.events.MenuEvent;

            [Bindable]
            private var menu:Menu;

            private function initMenu():void {
                menu = new Menu();
                menu.dataProvider = arr;
            }
        
]]>
    
</mx:Script>

    
<mx:Array id="arr">
        
<mx:Object label="Button" />
        
<mx:Object label="ButtonBar" />
        
<mx:Object label="ColorPicker" />
        
<mx:Object label="ComboBox" />
    
</mx:Array>

    
<mx:Style>
        .myCustomPopUpStyleName {
           textAlign: left;
        }
    
</mx:Style>

    
<mx:ApplicationControlBar dock="true">
        
<mx:PopUpButton id="popUpButton"
                label
="Select a control…"
                popUp
="{menu}"
                popUpStyleName
="myCustomPopUpStyleName"
                preinitialize
="initMenu();" />
    
</mx:ApplicationControlBar>

</mx:Application>


Or, if a dynamic example is a bit more your style…

<?xml version="1.0"?>
<!-- http://blog.flexexamples.com/2008/01/18/aligning-labels-in-a-flex-popupbutton-controls-pop-up-menu/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout
="vertical"
        verticalAlign
="top"
        backgroundColor
="white">

    
<mx:Script>
        
<![CDATA[
            import mx.events.ItemClickEvent;
            import mx.controls.Menu;
            import mx.events.MenuEvent;

            [Bindable]
            private var menu:Menu;

            private function initMenu():void {
                menu = new Menu();
                menu.dataProvider = arr;
            }

            private function toggleButtonBar_itemClick(evt:ItemClickEvent):void {
                var obj:CSSStyleDeclaration = StyleManager.getStyleDeclaration(".myCustomPopUpStyleName");
                obj.setStyle("textAlign", evt.label);
                popUpButton.open();
            }
        
]]>
    
</mx:Script>

    
<mx:Array id="arr">
        
<mx:Object label="Button" />
        
<mx:Object label="ButtonBar" />
        
<mx:Object label="ColorPicker" />
        
<mx:Object label="ComboBox" />
    
</mx:Array>

    
<mx:Style>
        .myCustomPopUpStyleName {
           textAlign: left;
        }
    
</mx:Style>

    
<mx:ApplicationControlBar dock="true">
        
<mx:Form styleName="plain">
            
<mx:FormItem label="textAlign:">
                
<mx:ToggleButtonBar id="toggleButtonBar"
                        selectedIndex
="0"
                        itemClick
="toggleButtonBar_itemClick(event);">
                    
<mx:dataProvider>
                        
<mx:Array>
                            
<mx:Object label="left" />
                            
<mx:Object label="center" />
                            
<mx:Object label="right" />
                        
</mx:Array>
                    
</mx:dataProvider>
                
</mx:ToggleButtonBar>
            
</mx:FormItem>
        
</mx:Form>
    
</mx:ApplicationControlBar>

    
<mx:PopUpButton id="popUpButton"
            label
="Select a control"
            popUp
="{menu}"
            popUpStyleName
="myCustomPopUpStyleName"
            preinitialize
="initMenu();" />

</mx:Application>


    本文转自 OldHawk  博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2008/01/20/1045917.html,如需转载请自行联系原作者





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值