【问题标题】:GWT VerticalPanel not aligning to top after first widget第一个小部件后 GWT VerticalPanel 未与顶部对齐
【发布时间】:2021-05-29 05:27:41
【问题描述】:

我正在尝试创建这样的 GWT 结构:

   * RootLayoutPanel
   * --FlowPanel
   * ----VerticalSplitPanel left 
   * ------VerticalPanel top 
   * --------HorizontalPanel top 
   * ----------ListBox top 
   * --------TextArea top 
   * ------VerticalPanel bottom 
   * --------HorizontalPanel bottom 
   * ----------ListBox bottom 
   * --------TextArea bottom 

只要我忽略 TextAreas,一切都会正常工作。当我添加它们时,它们在 Horizo​​ntalPanel 下的 VerticalPanel 中居中,而不是与顶部对齐。复选框也一样。

如果我查看正在呈现的 HTML,我会在 Horizo​​ntalPanel 下看到填充。我试过了:

   leftTopHorizontalPanel.getElement().getStyle().setPadding(0,Unit.PX);

   leftTopTextArea.getElement().getStyle().setPadding(0,Unit.PX);

两者都没有做任何事情。我试过了:

   leftTopVerticalPanel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);

这也没有做任何事情。我认为默认情况下添加到 VerticalPanel 的所有小部件都会继续与顶部对齐?有什么建议吗?

编辑:这是一张显示我的意思的图片。请注意,复选框位于下拉列表下方的中心,而不是直接位于下拉列表下方。当我调整分隔线时,复选框保持居中。

这是创建图像的代码:

private FlowPanel createWatchlistsTab() {
    // FlowPanel will wrap its children as its width resizes with the window
    final FlowPanel flowPanel = new FlowPanel();

    // *********************** LEFT VERTICAL SPLIT PANEL ***************************
    SplitLayoutPanel leftVerticalSplitPanel = new SplitLayoutPanel();
    leftVerticalSplitPanel.setSize("30%", "600px");

    // SplitLayoutPanels render as a div. Divs are block elements which always start on a new line.
    // Hence they won't wrap in the FlowPanel unless you make them inline-block, as below.
    leftVerticalSplitPanel.getElement().getStyle().setDisplay(Display.INLINE_BLOCK);

    // create the toolbar at the top of the left VerticalSplitPanel
    HorizontalPanel leftTopHorizontalPanel = new HorizontalPanel();
    leftTopHorizontalPanel.setWidth("100%");
    leftTopHorizontalPanel.setHeight("25px");
    // leftTopHorizontalPanel.getElement().getStyle().setPadding(0,Unit.PX);

    // create a ListBox, add items, then add it to the HorizontalPanel
    ListBox watchlistDropDown = new ListBox();
    watchlistDropDown.addItem("My Portfolio");
    watchlistDropDown.addItem("Buy Watch");
    leftTopHorizontalPanel.add(watchlistDropDown);

    // create dummy TextArea to be added to the VerticalPanel under the ToolBar
    TextArea leftTopTextArea = new TextArea();
    leftTopTextArea.setWidth("98%");
    leftTopTextArea.setHeight("100%");
    leftTopTextArea.setText("dummy text to show left top widget");
    leftTopTextArea.getElement().getStyle().setPadding(0, Unit.PX);

    // create a VerticalPanel and add the HorizontalPanel and TextArea
    VerticalPanel leftTopVerticalPanel = new VerticalPanel();
    // leftTopVerticalPanel.setVerticalAlignment(VerticalPanel.ALIGN_TOP);
    leftTopVerticalPanel.setWidth("100%");
    leftTopVerticalPanel.setHeight("100%");
    leftTopVerticalPanel.add(leftTopHorizontalPanel);
    CheckBox cb = new CheckBox("checkbox");
    leftTopVerticalPanel.add(cb);
    // leftTopVerticalPanel.add(leftTopTextArea);

    // add the VerticalPanel to the left top SplitPanel
    leftVerticalSplitPanel.addNorth(leftTopVerticalPanel, 200);
    int minWidth = 200;
    // leftVerticalSplitPanel.setWidgetMinSize(leftTopVerticalPanel, minWidth);
    leftVerticalSplitPanel.setWidgetMinSize(leftTopVerticalPanel, minWidth);

    // add notes TextArea to left bottom VerticalSplitPanel
    TextArea leftBottomTextArea = new TextArea();
    leftBottomTextArea.setWidth("98%");
    leftBottomTextArea.setHeight("100%");
    leftBottomTextArea.setText("dummy text to show left bottom widget");
    leftVerticalSplitPanel.add(leftBottomTextArea);

    // add the left VerticalSplitPanel to the FlowPanel
    flowPanel.add(leftVerticalSplitPanel);

【问题讨论】:

  • 通过创建类似的结构,我无法复制您提到的行为。请将您的代码添加到问题中,并可能附上屏幕截图,说明您想要实现的更改。
  • 我添加了一张图片。我会努力获取相关代码。

标签: javascript java html css gwt


【解决方案1】:

VerticalPanel (leftTopVerticalPanel) 被渲染为<table>

您将其高度设置为 100%:

leftTopVerticalPanel.setHeight("100%");

这使它填满了SplitLayoutPanel (leftVerticalSplitPanel) 北部的所有可用空间。

所以<table> 被垂直拉伸,因此它的行也被均匀拉伸。

参见下面的 DOM 结构:


setHeight("100%") 注释掉该行,您的复选框将直接位于下拉列表下方:

【讨论】:

  • 谢谢亚当。我将 VerticalPanel 的高度设置为在移动分隔线时扩展或收缩。因此,我认为应该将其设置为 100%。我不明白为什么不应该。但你是对的,它有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-28
  • 1970-01-01
  • 2015-09-28
  • 2018-08-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多