Flex 4.5 - Spark DropDownList - DropDown的最小宽度是锚点的宽度

时间:2011-07-29 19:33:18

标签: flex drop-down-menu width skin flex-spark

我正在使用spark DropDownLists,我不希望在下拉列表中看到任何水平滚动条。我很好,下拉比锚更宽,所以我有一个自定义皮肤,将popUpWidthMatchesAnchorWidth设置为false。但我不希望下拉列表比锚点小。这是我的困境。

我提出了一个有时有效的解决方案,但它有些问题。我的DropDownList皮肤如下:

<?xml version="1.0" encoding="utf-8"?>
<s:SparkSkin xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:fb="http://ns.adobe.com/flashbuilder/2009" alpha.disabled=".5" minHeight="25"> 

    <fx:Metadata>
    <![CDATA[ 
        [HostComponent("spark.components.DropDownList")]
    ]]>
    </fx:Metadata>

    <fx:Script>
        <![CDATA[
            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                // anchor width is the min width of the dropdown
                if (dropDown && openButton && popUp && dropDown.getExplicitOrMeasuredWidth() < openButton.getExplicitOrMeasuredWidth())
                    popUp.popUpWidthMatchesAnchorWidth = true;

                super.updateDisplayList(unscaledWidth, unscaledHeight);
            }
        ]]>
    </fx:Script>

    <s:states>
        <s:State name="normal" stateGroups="closed" />
        <s:State name="open" />
        <s:State name="disabled" stateGroups="closed" />
    </s:states>

    <s:PopUpAnchor id="popUp" displayPopUp.normal="false" displayPopUp.open="true" includeIn="open"
        left="0" right="0" top="0" bottom="0" itemDestructionPolicy="auto"
        popUpPosition="below" popUpWidthMatchesAnchorWidth="false">

        <s:Group id="dropDown" maxHeight="134" minHeight="22" >

            <s:RectangularDropShadow id="dropShadow" blurX="20" blurY="20" alpha="0.45" distance="7" 
                 angle="90" color="#000000" left="0" top="0" right="0" bottom="0"/>

            <s:Rect id="border" left="0" right="0" top="0" bottom="0">
                <s:stroke>
                    <s:SolidColorStroke id="borderStroke" weight="1"/>
                </s:stroke>
            </s:Rect>

            <s:Rect id="background" left="1" right="1" top="1" bottom="1" >
                <s:fill>
                    <s:SolidColor color="#222222"/>
                </s:fill>
            </s:Rect>

            <s:Scroller id="scroller" left="0" top="0" right="0" bottom="0" hasFocusableChildren="false" minViewportInset="1">
                <s:DataGroup id="dataGroup" itemRenderer="spark.skins.spark.DefaultItemRenderer">
                    <s:layout>
                        <s:VerticalLayout gap="0" horizontalAlign="contentJustify"/>
                    </s:layout>
                </s:DataGroup> 
            </s:Scroller>
        </s:Group>
    </s:PopUpAnchor>

    <s:Button id="openButton" left="0" right="0" top="0" bottom="0" focusEnabled="false" alpha.disabled="0.5"
        skinClass.closed="assets.skins.dropDownList.dropDownListNormalButtonSkin"
        skinClass.open="assets.skins.dropDownList.dropDownListOpenButtonSkin"/>  

    <s:Label id="labelDisplay" verticalAlign="middle" maxDisplayedLines="1" 
        mouseEnabled="false" mouseChildren="false"
        left="7" right="30" top="2" bottom="2" width="75" verticalCenter="1" /> 

</s:SparkSkin>

当下拉列表的内容比锚点宽或小于约100px时,这可以正常工作,但是如果我明确地将DropDownList实例中的宽度设置为200px,并且下拉列表的内容是大约150px宽,下拉小于锚。

基本上,看起来openButton.getExplicitOrMeasuredWidth()返回大约100,无论我将DropDownList的宽度设置为什么。

如何更改此设置以便我可以使用多个不同宽度的DropDownLists,但始终确保下拉列表的宽度大于或等于锚点的宽度?

2 个答案:

答案 0 :(得分:3)

你说: “但如果我明确地将DropDownList实例中的宽度设置为200px,并且下拉列表的内容大约为150px宽,则下拉列表小于锚点。”

因此,如果您明确设置宽度,则this.width始终可靠。只需用this.width替换button.getExplicitOrMeasuredWidth(),问题就解决了。

if (dropDown && openButton && popUp && dropDown.getExplicitOrMeasuredWidth() < this.width)
                popUp.popUpWidthMatchesAnchorWidth = true;

答案 1 :(得分:2)

我建议您使用openButton.width代替openButton.getExplicitOrMeasuredWidth()