2013-06-24 53 views
0

如何在重新定位onLayout按鈕時對齊按鈕文本?我有一個圖表,其中有許多橫軸每個軸有三個按鈕按以下順序排列enter image description here在Android中重新定位按鈕onLayout後對齊按鈕文本

現在我必須重新定位,以便圖上的所有按鈕正確對齊,並放置在我需要的地方。除了藍色按鈕的文本對齊,每件事情都很好。如果我說引力爲中心,則它將高度視爲藍色和紅色按鈕的組合高度,並將文本放在紅色按鈕後面一半。如果在黑色區域沒有任何聲音,我們只在按鈕左端有藍色小箭頭,但是我希望文本在重力底部和cent_horizo​​ntal的藍色區域。有趣的是我的重力底部和中心水平對於平板電腦很好。

這裏是我的OnLayout代碼:

protected void onLayout(boolean changed, int l, int t, int r, int b) { 
    super.onLayout(changed, l, t, r, b); 


    //this.strikeButton.layout(left,top,right,bottom); 
    int height = this.getMeasuredHeight(); 
    int width = this.getMeasuredWidth(); 
    int buttonWidth = this.upArrowButton.getMeasuredWidth(); 
    int buttonHeight = (2*height)/5; 
    int right = width - buttonWidth; 

    int left = 0; 
    int top = height/2; 
    top -= buttonHeight/2; 

    int bottom = top + buttonHeight; 
    this.strikePriceButton.layout(left, top, right, bottom); 
    this.upArrowButton.layout(left, 0, right, height/2); 
    this.downArrowButton.layout(left, height/2, right, height); 
    this.upArrowButton.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL); 
    this.downArrowButton.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL); 
    this.strikePriceButton.setGravity(Gravity.CENTER); 
    } 

這裏是我的OnMeasure功能:

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    this.setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), App.isTablet(context) ? 96 : 128); 
} 

我使用相同的高度,平板電腦和手機它看起來並不好其中一個,所以我嘗試必須使用不同的高度。

回答

0

只需使用dp而不是使用高度和寬度值。