繁体   English   中英

隐藏GridLayout列和剩余的列间距

[英]Hide GridLayout column and remaining column spacing

tl; dr有什么好方法可以在GridLayout隐藏列,还可以在父Composite上隐藏剩余的列间距?


我在GridLayout有几列的复合。 一列是带有StackLayoutComposite ,因此只要执行一些操作,我就可以隐藏/显示Composite

例如: 在此处输入图片说明

为了进行隐藏/显示,我一直在GridData上设置exclude属性,并在topControl上设置StackLayout属性:

// To hide
stackLayout.topControl = null;
stackComposite.setVisible(false);
((GridData) stackComposite.getLayoutData()).exclude = true;

现在,当我隐藏中间的Composite ,我看到行的远端出现了额外的空间。

在此处输入图片说明

大概是因为基本Composite上的GridLayout仍然具有相同的列数,因此我们看到的是宽度为0的列,并且在任一侧都具有指定的列间距。

为了解决这个问题,我提出了几种(非理想的)解决方案:

  1. 隐藏/显示时减少/增加列数。

  2. 而不是使用exclude属性,我们使用StackLayout覆盖Composite并更改computeSize(...)函数以返回0,或者根据是否希望它出现来计算实际大小。 (到目前为止,这是我最不喜欢的选项,即使编写它,我也很难过)

  3. 将基础Composite上的horizontalSpacing设置为0,而是使用Composite每列内的间距来指定间距。 隐藏时看起来像这样:

在此处输入图片说明

到目前为止, 选项1一直是最诱人的,但是在尝试保持代码模块化时,这里存在明显的缺陷。 例如,如果每个列的Composite由某个可重用的组件设置,则增加/减少列数取决于该组件,知道其父Composite具有GridLayout不是一个安全的假设!

选项2我放心不喜欢。

选项3并不可怕,但是仍然感觉像是滥用间距和边距。 另外,回到模块化的角度来看,其他列组件将是负责的间距,这会影响较大的视图,而不是让较大的视图决定间距。

因此,我的问题归结为: 还有没有比这三个更好的选择了?


MCVE用来获取上面的图像:

public class MCVE {

    final Display display;
    final Shell shell;

    public MCVE() {
        display = new Display();
        shell = new Shell(display);
        shell.setLayout(new GridLayout());
        shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

        final Composite baseComposite = new Composite(shell, SWT.NONE);
        baseComposite.setLayout(new GridLayout(3, false));
        baseComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        baseComposite.setBackground(new Color(display, new RGB(125, 125, 255)));

        final Composite contentComposite1 = new Composite(baseComposite, SWT.NONE);
        contentComposite1.setLayout(new GridLayout());
        contentComposite1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Label contentLabel1 = new Label(contentComposite1, SWT.NONE);
        contentLabel1.setText("I stretch to fill available space!");

        final Composite stackComposite = new Composite(baseComposite, SWT.NONE);
        final StackLayout stackLayout = new StackLayout();
        stackComposite.setLayout(stackLayout);
        stackComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));

        final Composite stackContentComposite = new Composite(stackComposite, SWT.NONE);
        stackContentComposite.setLayout(new GridLayout());
        stackContentComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
        final Label stackContentLabel = new Label(stackContentComposite, SWT.NONE);
        stackContentLabel.setText("I can disappear and reappear!");

        stackLayout.topControl = stackContentComposite;

        final Composite contentComposite3 = new Composite(baseComposite, SWT.NONE);
        contentComposite3.setLayout(new GridLayout());
        contentComposite3.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, true));
        final Label contentLabel3 = new Label(contentComposite3, SWT.NONE);
        contentLabel3.setText("I live on the end :(");

        final Button flipButton = new Button(shell, SWT.PUSH);
        flipButton.setText("Flip");
        flipButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(final SelectionEvent e) {
                if (stackLayout.topControl == null) {
                    stackLayout.topControl = stackContentComposite;
                    ((GridData) stackComposite.getLayoutData()).exclude = false;
                    // ((GridLayout) baseComposite.getLayout()).numColumns++;
                    stackComposite.setVisible(true);
                    baseComposite.layout(true, true);
                } else {
                    stackLayout.topControl = null;
                    ((GridData) stackComposite.getLayoutData()).exclude = true;
                    // ((GridLayout) baseComposite.getLayout()).numColumns--;
                    stackComposite.setVisible(false);
                    baseComposite.layout(true, true);
                }
            }
        });
    }

    public void run() {
        shell.setSize(600, 115);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }

    public static void main(final String... args) {
        new MCVE().run();
    }

}

我没有其他办法了。

选项1绝对是我要使用的选项,并且已被Eclipse代码广泛使用(例如,对话框按钮栏上的按钮)。

由于您已经假定控件布局数据是GridData因此可以安全地假定父布局是GridLayout (将GridData设置为其他布局上的布局数据通常会产生运行时错误)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM