2013-02-26 141 views
1

我正在致力於Flex mobil e應用程序,並且一直在優化其在移動設備上的性能。我已閱讀皮膚必須寫在ActionScript,所以我試圖創建我的AS ButtonSkinFlex移動按鈕在ActionScript中的皮膚 - 多行標籤Display

public class mButtonSkin extends ButtonSkin 
{ 
    private static var colorMatrix:Matrix = new Matrix(); 

    protected var radiusX:Number = 12; 
    protected var mAngle:Number = 30; 
    protected var labelWidth:Number; 
    protected var labelHeight:Number; 
    protected var paddingLeft:Number = 12; 
    protected var paddingRight:Number = 12; 
    protected var paddingTop:Number = 2; 
    protected var paddingBottom:Number = 2; 



    public function mButtonSkin() { 
     super(); 
    } 


    override protected function drawBackground(unscaledWidth:Number,unscaledHeight:Number) : void { 
     colorMatrix.createGradientBox(width,height,mAngle); 
     if (currentState == "up") { 
      graphics.beginGradientFill(GradientType.LINEAR, 
       [hostComponent.getStyle("fill3"),hostComponent.getStyle("fill1")], [1,1], [0,255], colorMatrix); 
     } else { 
      graphics.beginFill(hostComponent.getStyle("fill4")); 
     } 

     graphics.lineStyle(0.5,hostComponent.getStyle("borderColor"),0.5); 
     graphics.drawRoundRect(0,0,hostComponent.width,hostComponent.height,12); 
     graphics.endFill(); 
    } 


    override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number) : void { 
     labelDisplay.wordWrap = true; 
     labelDisplay.multiline = true; 

     labelDisplay.commitStyles(); 

     labelWidth = width - paddingLeft - paddingRight; 
     labelHeight = height - paddingTop - paddingBottom; 

     var x:Number = width/2 - labelWidth/2; 
     var y:Number = height/2 - getElementPreferredHeight(labelDisplay)/2; 

     setElementSize(labelDisplay,labelWidth,labelHeight); 
     setElementPosition(labelDisplay,x,y); 
    } 


    override protected function labelDisplay_valueCommitHandler(event:FlexEvent) : void { 
     labelDisplayShadow.text = ""; 
    } 
} 

應用這種皮膚在我的按鈕:

enter image description here

按鈕labelDisplay的有錯的比對(verticalAlignment必須是中間)。

點擊按鈕後:

enter image description here

我的按鈕的labelDisplay現在是垂直對齊到中間。

我錯過了什麼嗎?我做錯了什麼?

回答

1

layoutContents功能,labelDisplay元件定位之前,我首先檢查它是否是numLines屬性大於1。這裏是我的更新功能:

override protected function layoutContents(unscaledWidth:Number, unscaledHeight:Number) : void { 
     labelDisplay.wordWrap = true; 
     labelDisplay.multiline = true; 

     labelWidth = width - paddingLeft - paddingRight; 
     labelHeight = height - paddingTop - paddingBottom; 

     var x:Number = width * .5 - labelWidth * .5; 
     var y:Number = height * .5 - getElementPreferredHeight(labelDisplay) * .5; 

     setElementSize(labelDisplay,labelWidth,labelHeight); 

     if (labelDisplay.numLines == 1) 
      setElementPosition(labelDisplay,x,y); 
     else 
      setElementPosition(labelDisplay, x, labelDisplay.getLayoutBoundsY()); 
    } 

這種解決方案可能不會對其他開發者有用的在那裏但它解決了我的問題。

+0

請注意,我已將/ 2更改爲* .5。這是因爲我讀過一些博客,其中的乘法運算速度比分區快。 :)無論如何,只是想分享這一點。 – wens 2013-04-17 10:34:19