2010-06-12 52 views
0

我創建了一個自定義按鈕,我將一堆Custombuttons放置在verticalfieldManager中,我已將verticalField Manager放在中間。當我創建一個默認的buttonField時,verticalfield Manager可以對齊中心的buttonfield。但是當我在verticalField Manager中分配自定義按鈕時,它不會在中心對齊。CustomButton字段不在中心對齊

這裏是我的custombuttoncode

public CustomButtonField(String label,long style) { 
    super(style); 
    this.label = label; 
    onPicture = Bitmap.getBitmapResource(onPicturePath); 
    font = getFont(); 
    this.setPadding(5,5, 5, 5); 
} 

public String getLabel() { 
    return label; 
} 

public int getPreferredHeight() { 
    return onPicture.getHeight(); 
} 

public int getPreferredWidth() { 
    return onPicture.getWidth(); 
} 

protected void layout(int width , int height) { 
    setExtent(Math.min(width, Display.getWidth()), Math.min(height,getPreferredHeight())); 
} 

protected void paint(Graphics graphics) { 
    int texty =(getHeight()-getFont().getHeight())/2; 

    if (isFocus()) { 
     graphics.setColor(Color.BLACK); 
     graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0); 
     graphics.setColor(Color.WHITE); 
     graphics.setFont(font); 
     graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth()); 
    } else { 
     graphics.drawBitmap(0, 0, getWidth(), getHeight(),onPicture , 0, 0); 
     graphics.setColor(Color.WHITE); 
     graphics.setFont(font); 
     graphics.drawText(label,0,texty,DrawStyle.ELLIPSIS,getWidth()); 
    } 
} 

public boolean isFocusable() { 
    return true; 
} 

protected void onFocus(int direction) { 
    super.onFocus(direction); 
    invalidate(); 
} 

protected void onUnfocus() { 
    super.onUnfocus(); 
    invalidate(); 
} 

protected boolean navigationClick(int status, int time) { 
    fieldChangeNotify(0); 
    return true; 
} 

protected boolean keyChar(char character, int status, int time) { 
    if (character == Keypad.KEY_ENTER) { 
     fieldChangeNotify(0); 
     return true; 
    } 
    return super.keyChar(character, status, time); 
} 

}

回答

2

當你創建你需要通過Field.FIELD_HCENTER標誌作爲你的風格標誌之一的按鈕。

CustomButtonField button = new CustomButtonField("TestLabel", Field.FIELD_HCENTER);