我有一个包含 3 列的 GridFieldManager,我想像这样对齐它们的内容:
|标题左 | 标题中心 | 标题权|
问题是我使用的是 RichTextField 而不是 LabelField 因为我希望每个标题的文本如下所示:
|标题左 | 标题中心 | 标题权 | |被包裹 | 也包起来了| 还包裹|
如果我使用 RichTextField 而不是 LabelField,则忽略对齐。这是我的代码:
public class CustomGridFieldextends GridFieldManager {
private int numColumns = 3;
private int margin = 5;
public CustomGridField(String leftText, String centerText, String rightText) {
super(1, 3, GridFieldManager.USE_ALL_WIDTH);
setPadding(0, margin, 0, margin);
int columnWidth = (Display.getWidth() / numColumns);
for (int i = 0; i < numColumns; i++) {
setColumnProperty(i, GridFieldManager.FIXED_SIZE, columnWidth);
}
// leftmost text
RichTextField leftLabel = new RichTextField(leftText){
protected void paint(Graphics g) {
g.setColor(Color.WHITE);
g.setFont(getFont().derive(Font.BOLD));
super.paint(g);
}
};
leftLabel.setFont(getFont().derive(Font.BOLD));
add(leftLabel, Field.FIELD_LEFT);
// center text
RichTextField centerLabel = new RichTextField(centerText){
protected void paint(Graphics g) {
g.setFont(getFont().derive(Font.BOLD));
g.setColor(Color.WHITE);
super.paint(g);
}
};
centerLabel.setFont(getFont().derive(Font.BOLD));
add(centerLabel, Field.FIELD_HCENTER);
// rightmost text
RichTextField rightLabel = new RichTextField(rightText) {
protected void paint(Graphics g) {
g.setFont(getFont().derive(Font.BOLD));
g.setColor(Color.WHITE);
super.paint(g);
}
};
rightLabel.setFont(getFont().derive(Font.BOLD));
add(rightLabel, Field.FIELD_RIGHT);
}
protected void paintBackground(Graphics g) {
// draw a nice background...
}
}