使用等高的行创建 GridLayout

Create GridLayout With Rows of Equal Height

我想合成一些标签,所有标签的高度应该相同,但它们应该垂直居中。

到目前为止我有以下内容:

public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.VERTICAL);
    composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(true).spacing(0, 0).create());
    for (int i = 0; i < 10; i++) {
        final Label label = new Label(composite, SWT.WRAP);
        label.setAlignment(SWT.CENTER);
        label.setText(i < 5 ? "small " + i : "a very big text for the row that is named " + i);
        label.setToolTipText(label.getText());
        label.setLayoutData(GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).create());
    }

    shell.setSize(100, 600);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

如您所见,标签在其行中垂直居中,但环绕的标签比其他标签占用更多 space。

如果我将 hint(SWT.DEFAULT, 60) 添加到 GridData,我可以强制标签具有相同的高度,但它们将不再垂直居中。

我可能可以将所有标签包裹在复合材料中,在复合材料上设置高度提示并使标签居中,但让我们先看看还有另一种选择。

如何创建等高的垂直居中行?

由于您试图统一排列大小可能不同的小部件,我认为一个不错的选择是将每个 Label 放入一个 Composite。这样每个 Composite 都可以具有相同的大小,并且 Label 将在其中居中。

通过在parentComposite中添加一个ControlListener,可以查看每个childComposite的大小,然后设置高度提示每个 child 是最大的 child:

的高度
public static void main(final String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());

    final Composite composite = new Composite(shell, SWT.VERTICAL);
    composite.setLayout(GridLayoutFactory.fillDefaults().numColumns(1).equalWidth(true).spacing(0, 0).create());

    final List<Composite> children = new ArrayList<Composite>();
    for (int i = 0; i < 10; i++) {
        final Composite c = new Composite(composite, SWT.BORDER);
        c.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        c.setLayout(new GridLayout());

        final Label label = new Label(c, SWT.WRAP);
        label.setAlignment(SWT.CENTER);
        label.setText(i < 5 ? "small " + i : "a very big text for the row that is named " + i);
        label.setToolTipText(label.getText());
        label.setLayoutData(GridDataFactory.fillDefaults().align(SWT.CENTER, SWT.CENTER).grab(true, true).create());

        children.add(c);
    }

    composite.addControlListener(new ControlAdapter() {
        @Override
        public void controlResized(final ControlEvent e) {
            int maxHeight = 0;
            for (final Composite c : children) {
                if (c.getClientArea().height > maxHeight) {
                    maxHeight = c.getClientArea().height;
                }
            }
            for (final Composite c : children) {
                ((GridData) c.getLayoutData()).heightHint = maxHeight;
            }
        }
    });

    shell.setSize(100, 600);
    shell.open();

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
}

结果:

调整大小后: