如何使用大字体居中LabelField

时间:2012-09-17 16:00:55

标签: java blackberry java-me

我需要使用垂直场管理器水平居中LabelField。我还需要增加这个LabelField的字体大小。我发现,一旦我使用FIELD_HCENTER居中,如果我改变字体的大小,它会改变字段的位置。

这是代码

VerticalFieldManager vfm = new VerticalFieldManager(Field.FIELD_VCENTER | Manager.USE_ALL_WIDTH);
LabelField title = new LabelField("My Title",Field.FIELD_HCENTER) ;
title.setFont(Font.getDefault().derive(Font.BOLD, 18, Ui.UNITS_pt);
vfm.add(title);

(如果我删除第三行title.setFont,那么标题居中,否则,它不是)。我需要一些帮助来获得解决方案。 任何帮助表示赞赏? 感谢

1 个答案:

答案 0 :(得分:1)

您可以通过覆盖sublayout VerticalFieldManager方法来创建自定义FieldManger,该方法会将其子项放在中心位置。以下是一个这样的实现。

class CustomVerticalFieldManger extends VerticalFieldManager {
        private final int VERTICAL_GAP = 30;

        protected void sublayout(int maxWidth, int maxHeight) {
            int nFields = getFieldCount(), height = 0, xPosition, yPosition = 0;
            Field f;
            for (int i = 0; i < nFields; i++) {
                f = getField(i);
                layoutChild(f, maxWidth, maxHeight);
                height += f.getHeight();
                if (i != 0) {
                    height += VERTICAL_GAP;
                }
            }
            for (int i = 0; i < nFields; i++) {
                f = getField(i);
                xPosition = (maxWidth - f.getWidth()) / 2;
                setPositionChild(f, xPosition, yPosition);
                yPosition += f.getHeight();
                if (i != nFields - 1) {
                    yPosition += VERTICAL_GAP;
                }
            }
            setExtent(maxWidth, height);
        }
    }

用法

class MyScreen extends MainScreen {
    private LabelField lblOne, lblTwo, lblThree;

    public MyScreen() {
        CustomVerticalFieldManger cvfm = new CustomVerticalFieldManger();
        cvfm.setBackground(BackgroundFactory.createSolidBackground(Color.AQUA));

        lblOne = new LabelField("The quick brown fox jumps over the lazy dog");
        lblTwo = new LabelField("The quick brown fox jumps over the lazy dog");
        lblThree = new LabelField("The quick brown fox jumps over the lazy dog");

        cvfm.add(lblOne);
        cvfm.add(lblTwo);
        cvfm.add(lblThree);

        add(cvfm);

        changeFont();
    }

    private int fontSize = 5;

    private void changeFont() {
        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            public void run() {
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        if (fontSize == 50) {
                            fontSize = 5;
                        }
                        lblOne.setFont(Font.getDefault().derive(Font.PLAIN,
                                fontSize));
                        lblTwo.setFont(Font.getDefault().derive(Font.PLAIN,
                                fontSize + 2));
                        lblThree.setFont(Font.getDefault().derive(Font.PLAIN,
                                fontSize + 4));
                        fontSize += 6;
                        updateLayout();
                        invalidate();
                    }
                });
            }
        };

        timer.schedule(task, 500, 1000);
    }
}