0

这是我的自定义布局,与 android 相同linear layout

public class Custom_TopField extends VerticalFieldManager {
    private static final int FIELD_HEIGHT = 70;

    private String _text;

    Custom_TopField(int color, String text) {
        super(Manager.NO_VERTICAL_SCROLL);
        _text = text;

        Background background = BackgroundFactory.createSolidBackground(color);
        setBackground(background);
    }

    protected void sublayout(int width, int height) {
        width = Math.min(width, getPreferredWidth());
        height = Math.min(height, getPreferredHeight());
        setExtent(width, height);
    }

    public int getPreferredHeight() {
        return FIELD_HEIGHT;
    }

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

    public void paint(Graphics graphics) {
        int rectHeight = getPreferredHeight();
        int rectWidth = getPreferredWidth();

        Font font = Font.getDefault().derive(Font.BOLD, 65);
        graphics.drawRect(0, 0, rectWidth, rectHeight);
        graphics.drawText(_text, rectWidth / 2, 10);
        graphics.setFont(font);
        graphics.setColor(Color.WHITE);
        super.paint(graphics);
    }
}

输出=背景红色和字体=黑色

我想要的是背景红色,字体白色,字体大小 40。

我也想知道如何以编程方式创建小部件?

4

1 回答 1

0

要将字体设置为白色并将大小设置为 40,请按如下方式更改绘制:

public void paint(Graphics graphics) {
    int rectHeight = getPreferredHeight();
    int rectWidth = getPreferredWidth();

    Font font = Font.getDefault().derive(Font.BOLD, 40);
    graphics.setColor(Color.WHITE);
    graphics.setFont(font);
    graphics.drawRect(0, 0, rectWidth, rectHeight);
    graphics.drawText(_text, rectWidth / 2, 10);
    super.paint(graphics);
}
于 2012-06-04T08:38:17.507 回答