我不熟悉css,遗憾的是我没有太多时间对此进行调查,所以我选择了内置Vaadin ValoTheme
。
我有一个带有两个对象的verticallayout - 标签和面板,两个组件都应居中。这就是我想要实现的目标:
不幸的是,经过深思熟虑的尝试,我得到了这个
我的代码:
public class SplashScreen extends VerticalLayout implements View {
private static final int PANEL_WIDTH = 320;
private static final int PANEL_HEIGHT = 140;
private static final int BUTTON_WIDTH = 270;
private static final int BUTTON_HEIGHT = 70;
private ComponentHelper componentHelper;
private Panel panel;
private VerticalLayout formLayout;
private Label welcome;
private Button toLoginPage;
public SplashScreen() {
initComponents();
buildSplashView();
}
protected void initComponents() {
componentHelper = ComponentHelper.getInstance();
panel = componentHelper.createPanel("", PANEL_WIDTH, PANEL_HEIGHT);
welcome = componentHelper.createH3Label("Welcome");
formLayout = componentHelper.createVerticalLayout();
toLoginPage = componentHelper.createFriendlyButton("To Login Page", BUTTON_WIDTH, BUTTON_HEIGHT);
toLoginPage.addClickListener(this::redirect);
}
private void buildSplashView() {
addComponent(panel);
addComponent(welcome);
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
panel.setContent(formLayout);
panel.setStyleName(ValoTheme.PANEL_BORDERLESS);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setHeight(100, Unit.PERCENTAGE);
}
public void redirect(Button.ClickEvent event) {
try {
Thread.sleep(Constants.TRANSITION_TIME_DELAY);
getUI().getNavigator().navigateTo(ViewTokens.SIGNIN);
} catch (InterruptedException e) {
}
}
@Override
public void enter(ViewChangeListener.ViewChangeEvent viewChangeEvent) {
}
}
我做错了什么?我无法完全理解如何正确设计布局(在提出这个问题之前,我读过Vaadin书)
注意:' componentHelper'用于创建对象时使用一些最小的ValoTheme样式。
感谢您的建议
UPDATE:使用welcome.setSizeUndefined();
的结果更新2:修改了@qtdzz代码以重现问题:
private void buildSplashView() {
addComponent(welcome);
addComponent(text1);
addComponent(panel);
setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
setComponentAlignment(text1, Alignment.MIDDLE_CENTER);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setSizeFull();
panel.setSizeUndefined();
panel.setContent(formLayout);
welcome.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}
答案 0 :(得分:1)
在我看来,如果将欢迎标签放在formLayout中并为其设置对齐方式会更容易。
这是我的建议,希望对此有所帮助:
private void buildSplashView() {
addComponent(panel);
setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
setSizeFull();
panel.setContent(formLayout);
panel.setStyleName(ValoTheme.PANEL_BORDERLESS);
formLayout.addComponent(welcome);
welcome.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}
UPDATE1:根据您的代码并根据我的理解,可以通过以下方式解释一个解决方案。如果你将“text1”,“welcome”和“panel”添加到其中,它将看起来像下面的图片: 因此,为了使它们与中间对齐,我们需要将“text1”和“panel”的expandRatio设置为0.5,将“welcome”设置为0。然后将“text1”设置为Alignment.BOTTOM_CENTER,“welcome”为Alignment.MIDDLE_CENTER,“panel”为Alignment.TOP_CENTER,而toLoginPage按钮为Alignment.TOP_CENTER。然后视图将如下所示: 上图代码:
private void buildSplashView() {
addComponent(text1);
addComponent(welcome);
addComponent(panel);
setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
setComponentAlignment(text1, Alignment.BOTTOM_CENTER);
setComponentAlignment(panel, Alignment.TOP_CENTER);
setSizeFull();
setExpandRatio(text1,0.5f);
setExpandRatio(panel,0.5f);
panel.setSizeUndefined();
panel.setContent(formLayout);
welcome.setSizeUndefined();
text1.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.TOP_CENTER);
formLayout.setSizeFull();
}
顺便说一下,我觉得很难维护和理解上面的解决方案(例如,如果你想添加另一个文本,你必须在text1和welcome之间添加它)。我推荐的解决方案是使用一个包装器布局(垂直布局)来包装所有组件并将其设置为基本布局的middle_center。解决方法是:
private void buildSplashView() {
wrapperLayout.addComponent(text1);//wrapperLayout is a vertical Layout
wrapperLayout.addComponent(welcome);
wrapperLayout.addComponent(panel);
wrapperLayout.setComponentAlignment(welcome, Alignment.MIDDLE_CENTER);
wrapperLayout.setComponentAlignment(text1, Alignment.MIDDLE_CENTER);
wrapperLayout.setComponentAlignment(panel, Alignment.MIDDLE_CENTER);
addComponent(wrapperLayout);
setComponentAlignment(wrapperLayout, Alignment.MIDDLE_CENTER);
setSizeFull();
panel.setSizeUndefined();
panel.setContent(formLayout);
welcome.setSizeUndefined();
text1.setSizeUndefined();
formLayout.addComponent(toLoginPage);
formLayout.setComponentAlignment(toLoginPage, Alignment.MIDDLE_CENTER);
formLayout.setSizeFull();
}
使用这种方法,您可以将任何组件添加到wrapperLayout中,它将自动对齐middle_center。
答案 1 :(得分:1)
VerticalLayout outerVLayout = new VerticalLayout();
outerVLayout.setSizeFull();
VerticalLayout innerVLayout = new VerticalLayout();
innerVLayout.setSizeUndefined();
outerVLayout.addComponent(innerVLayout);
outerVLayout.setComponentAlignment(innerVLayout, Alignment.MIDDLE_CENTER);
//add components to this inner vertical layout
innerVLayout.addComponent(label);
//textfields are fine but, in case of a label, remember to do this:
label.setSizeUndefined();
我希望它有所帮助。 : - )