0

我创建了一个自定义按钮,我将一堆 Custombuttons 放在一个 verticalfieldManager 中,我已将 verticalField Manager 对齐在中心。当我创建默认按钮域时,垂直域管理器能够将按钮域对齐在中心。但是当我在verticalField Manager 中分配custombuttonfield 时,它没有在中心对齐。

这是我的自定义按钮代码

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);
}

}

4

1 回答 1

2

创建按钮时,您需要将 Field.FIELD_HCENTER 标志作为样式标志之一传递。

CustomButtonField button = new CustomButtonField("TestLabel", Field.FIELD_HCENTER);
于 2010-06-21T13:45:35.910 回答