这是我的自定义布局,与 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。
我也想知道如何以编程方式创建小部件?