我的自定义verticalfieldmanger
public class Custom_TopField extends VerticalFieldManager {
private static final int FIELD_HEIGHT = 70;
private LabelField maintitle;
private String _text;
Custom_TopField(int color, String text) {
super(Manager.NO_VERTICAL_SCROLL);
_text = text;
Background background = BackgroundFactory.createSolidBackground(color);
setBackground(background);
maintitle = new LabelField(_text, Field.FIELD_VCENTER | Field.FIELD_HCENTER);
Font font = Font.getDefault().derive(Font.BOLD, 35);
maintitle.setFont(font);
add(maintitle);
}
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();
graphics.drawRect(0, 0, rectWidth, rectHeight);
super.paint(graphics);
}
}
虽然我add(maintitle)
但它并没有出现在垂直场的中间。
答案 0 :(得分:0)
你可以试试这个:
向构造函数添加标志:
super(Manager.NO_VERTICAL_SCROLL | Manager.USE_ALL_WIDTH);
同样到现场:
maintitle = new LabelField(DrawStyle.HCENTER | Field.FIELD_HCENTER);
这应该水平居中。您可以尝试添加标记以垂直居中,但很可能它不起作用,因为VFM存在垂直滚动问题。规则是:水平居中,使用VerticalFieldManager
,垂直居中,使用HorizontalFieldManager
。这就是你如何在垂直中心:
HorizontalFieldManager hfm = new HorizontalFieldManager(Manager.USE_ALL_HEIGHT);
Field toCenter = new <Field>(DrawStyle.VCENTER | Field.FIELD_VCENTER );
hfm.add(toCenter);
理论上你可以找出一种结合两种方法的解决方案,并将一个管理器嵌套在另一个管理器中。