0

我有一个包含 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...
    }
}
4

1 回答 1

1

找到了答案,原来我在创建 RichTextFields 时必须使用这种样式:

new RichTextField("text on left",RichTextField.TEXT_ALIGN_LEFT);
new RichTextField("text on center",RichTextField.TEXT_ALIGN_HCENTER);
new RichTextField("text on right",RichTextField.TEXT_ALIGN_RIGHT);

现在我正在研究grapping...

PD:在这里找到它:http: //v4ks1n.wordpress.com/2009/04/16/richtextfield-alignment/

于 2013-05-13T19:16:41.170 回答