1

我需要使用垂直字段管理器将 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,那么标题居中,否则,它不是)。我需要一些帮助来解决这个问题。任何帮助表示赞赏?谢谢

4

1 回答 1

1

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

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);
    }
}
于 2012-09-17T18:16:19.303 回答