2009-07-07 50 views
1

我要對齊的自定義按鈕字段(中星截圖)到市中心的一條水平經理的HFM黑莓 - 現場 - 水平對齊

final HorizontalFieldManager _navigation = new HorizontalFieldManager(HorizontalFieldManager.NO_VERTICAL_SCROLL | HorizontalFieldManager.FIELD_VCENTER | Field.USE_ALL_WIDTH) 
{  
protected void paint(Graphics graphics) 
{ 
    graphics.setGlobalAlpha(100); 

    int[] xInds = new int[]{0, getExtent().width, getExtent().width, 0}; 
    int[] yInds = new int[]{0, 0, getExtent().height, getExtent().height}; 
    final int[] cols = new int[]{Color.WHITE, Color.WHITE, color_computacenter_light_blue, color_computacenter_light_blue}; 

    graphics.drawShadedFilledPath(xInds, yInds, null, cols, null); 
    super.paint(graphics); 
} 
protected void sublayout(int maxWidth, int maxHeight) 
    { 
     super.sublayout(maxWidth, maxHeight); 
     setExtent(maxWidth, navigation_vertical_manager_height); 
    } 
}; 

alt text

定義

定製領域(部分)

public class NavigationButton extends Field 
{ 
    private String label; 
    Bitmap img; 
    int horizontalPadding = 4; 
    int verticalPadding = 10; 
    static int height; 
    static int weight; 
    ToolTip _tooltip; 

    public NavigationButton(String name, Bitmap img) 
    { 
     label = name; 
     this.img = img; 
     this.height = img.getHeight() + 4; 
     this.weight = img.getWidth() + 2; 
    } 

    public NavigationButton(String name, Bitmap img, long style) 
    { 
     super(style); 
     label = name; 
     this.img = img; 
     this.height = img.getHeight() + 4; 
     this.weight = img.getWidth() + 2; 
    } 

    protected void layout(int maxWidth, int maxHeight) 
    { 
     setExtent(maxWidth, maxHeight); 
     // setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight)); 
    } 

    protected void paint(Graphics graphics) 
    { 
     // Draw background 
     graphics.setGlobalAlpha(255); 
     if(isFocus()) 
     { 
      graphics.setColor(0x005AA1); 
      graphics.drawRoundRect(0 ,0 , weight + 1, height + 1, 2, 2); 
     } 
     if (img != null) 
     { 
      graphics.drawBitmap(0, 0 , img.getWidth(), img.getHeight(), img, 0, 0); 
     } 
    } 

[...]

任何想法,我做錯了什麼?!

謝謝!

+0

你能更明確的瞭解你想要什麼樣的行爲,以實現和你」真的看到了嗎?屏幕截圖很好! – 2009-07-07 18:07:52

回答

4

我沒有測試此代碼,但我懷疑你的問題是在你的領域的佈局方法:

protected void layout(int maxWidth, int maxHeight) 
    { 
     setExtent(maxWidth, maxHeight); 
     // setExtent(Math.min(weight, maxWidth), Math.min(height, maxHeight)); 
    } 

你有什麼評論說行的理由?我認爲發生的事情是您的自定義字段佔據了整個寬度,並且在繪製方法中,您正在最左側繪製位圖,使其看起來像字段左對齊。如果取消註釋該行,並在構建字段時添加Field.FIELD_HCENTER的字段樣式,則該字段應起作用。

或者,你可以保持的佈局是,剛繪製位圖的中心,通過改變你的paint方法行了這樣的事情

graphics.drawBitmap((this.getWidth()-img.getWidth())/2, 0 , img.getWidth(), img.getHeight(), img, 0, 0); 

領域仍將佔據整個寬度,但圖像將居中。這可能會導致觸摸屏設備出現意外行爲(即Storm)。

1

其實它很簡單。您必須使用Horizo​​ntalFieldManger和DrawStyle.Center。 這裏是代碼片段(如果你還沒有解決它尚未):

HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER); 
    ButtonField button = new ButtonField("Button",DrawStyle.HCENTER); 
    hfm.add(button); 

請, 赫爾沃耶