【发布时间】:2019-07-14 10:37:55
【问题描述】:
我使用 Vaadin 13 beta1 预发布版创建了一个正在表单中使用的自定义字段。但是,它在字段顶部创建了这个巨大的空白区域(请参阅下面的绿色标记,并忽略红色和黄色背景颜色,因为它们只是为了让我了解正在发生的事情。)
我该如何解决这个问题?我尝试手动设置 minHeight、maxHeight、height 值,但没有任何改变......这是我的自定义字段代码。 (formlayout 的代码是相当标准的,所以这里没有包含。)
package com.deepsearch.fe.util;
import com.vaadin.flow.component.customfield.CustomField;
import com.vaadin.flow.component.html.Label;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.textfield.TextField;
public class TextRangeFieldInt extends CustomField<StartToEndRangeInt> {
private final TextField start = new TextField();
private final Label lblTo = new Label("to");
private final TextField end = new TextField();
public TextRangeFieldInt(final StartToEndRangeInt startToEndRangeInt, final String startEndWidths) {
this.setHeight("10px");
this.setMinHeight("10px");
this.setMaxHeight("10px");
this.getElement().getStyle().set("background-color", "red");
//this.getStyle().set("background-color", "red");
setPresentationValue(startToEndRangeInt);
final HorizontalLayout hl = new HorizontalLayout();
hl.setWidth("100%");
hl.getStyle().set("background-color", "yellow");
hl.setMargin(false);
hl.setPadding(false);
hl.setAlignItems(FlexComponent.Alignment.CENTER);
lblTo.setWidth("1.5rem");
start.setWidth(startEndWidths);
end.setWidth(startEndWidths);
hl.add(start);
hl.add(lblTo);
hl.add(end);
add(hl);
}
@Override
protected StartToEndRangeInt generateModelValue() {
return new StartToEndRangeInt(Integer.parseInt(start.getValue()), Integer.parseInt(end.getValue()));
}
@Override
protected void setPresentationValue(StartToEndRangeInt newPresentationValue) {
start.setValue(newPresentationValue.getStartStr());
end.setValue(newPresentationValue.getEndStr());
}
}
在不同但相关的说明中,我这样做对吗?自定义字段还没有一个很好的例子(因为它仍处于测试阶段),所以我猜我应该使用水平布局来添加我的各个组件等。
更新 #1
基于下面的@Jouni 评论,我尝试创建一个更简单的表单布局,它只有几个“简单”字段(例如,所有文本字段,没有像复选框那样花哨的东西)。但问题依然存在。请参阅下面的屏幕截图以及表单布局的源代码。 (注意:我什至注释掉了表单布局的“responsiveStep”部分,以便只使用默认/简单场景。)
还有,这里是代码 sn-p(没什么花哨的):
final FormLayout nameLayout = new FormLayout();
// nameLayout.setResponsiveSteps(
// // new FormLayout.ResponsiveStep("0", 1),
//// new FormLayout.ResponsiveStep("21em", 2),
// new FormLayout.ResponsiveStep("0em", 3));
// nameLayout.add(uniqueExpName, sampleComplexity, gradientLength, numTechRepl, minMz, maxMz, enableVariableDiaWindow, densestMz, vlSampleCondit, useSettingsNextTime);
// nameLayout.add(uniqueExpName, sampleComplexity, gradientLength, numTechRepl, mzRange, enableVariableDiaWindow, densestMz, vlSampleCondit, useSettingsNextTime);
// nameLayout.add(uniqueExpName, sampleComplexity, gradientLength, numTechRepl, mzRange, enableVariableDiaWindow, densestMz, lblSampleAndConditionNumber, sampleAndCondNum);
nameLayout.add(uniqueExpName, gradientLength, numTechRepl, mzRange, densestMz);
nameLayout.setWidth("55rem");
add(nameLayout);
setAlignSelf(Alignment.CENTER, nameLayout);
【问题讨论】:
-
您对所有这些字段使用什么样的布局?我的假设是这是一个基线对齐问题(您的自定义字段的标签与其右侧复选框的标签对齐)。这意味着 CustomField 没有正确处理基线对齐(所以这是一个错误)。
-
@Jouni 我只使用普通的“formlayout”。但是,根据您的评论,我删除了复选框和所有其他字段,除了简单的“textFields”。我什至注释掉了“ResponsiveStep()”语句。您可以在上面我编辑的原始帖子中看到生成的屏幕截图和源代码 sn-p。 (问题仍然存在。)想法? (仅供参考:如果我将 CustomField 粘贴到 HorizontalLayout 中,到目前为止一切似乎都正常......)
-
是的,所以看起来肯定是基线对齐问题。作为一种解决方法,您可以将所有字段的对齐方式设置为
flex-start(即顶部)。 -
开发团队已经处理了这个问题:github.com/vaadin/vaadin-custom-field/issues/23