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);
}
}