I'm new to using Vaadin and have been trying to work out how I can make 2 Components be side by side when at full screen, but then stack on top of each other when the screen is mobile.
My current understanding is that a HorizontalLayout puts things side by side. And a VerticalLayout puts things on top of one another. So how do I go about using the functionality from both?
You need to look into using a different Layout type. Vaadin offers you a CssLayout and CustomLayout as well as the standard Vertical and Horizontal.
My personal favourite at the moment is using a CssLayout and then using a custom CSS Grid to make the components responsive.
Java:
@StyleSheet("MyStyleSheet.css")
public class ResponsiveLayout extends CssLayout {
private static final long serialVersionUID = -1028520275448675976L;
private static final String RESPONSIVE_LAYOUT = "responsive-layout";
private static final String LABEL_ONE = "label-one";
private static final String LABEL_TWO = "label-two";
private Label labelOne = new Label();
private Label labelTwo = new Label();
public ResponsiveLayout() {
config();
addComponents(labelOne, labelTwo);
}
private void config() {
addStyleName(RESPONSIVE_LAYOUT);
labelOne.addStyleName(LABEL_ONE);
labelTwo.addStyleName(LABEL_TWO);
}
}
CSS:
.responsive-layout {
display: grid !important;
grid-template-rows: auto;
grid-template-columns: 1fr 1fr;
display: -ms-grid !important; /* IE */
-ms-grid-rows: auto; /* IE */
-ms-grid-columns: 1fr 1fr; /* IE */
}
.label-one {
grid-column: 1;
-ms-grid-column: 1; /* IE */
}
.label-two {
grid-column: 2;
-ms-grid-column: 2; /* IE */
}
@media all and (max-width : 992px) {
.responsive-layout {
grid-template-columns: 1fr;
-ms-grid-columns: 1fr; /* IE */
}
.label-one {
grid-column: 1;
-ms-grid-column: 1; /* IE */
}
.label-two {
grid-column: 1;
-ms-grid-column: 1; /* IE */
}
}
You can use a Vaadin Add-on responsive layout. Using the grid system of flexboxgrid
@Override
public void enter(ViewChangeListener.ViewChangeEvent event) {
ResponsiveLayout responsiveLayout = new ResponsiveLayout();
responsiveLayout.setSizeFull();
ResponsiveRow rowOne = responsiveLayout.addRow();
Button deleteButton = new Button("", VaadinIcons.TRASH);
deleteButton.addStyleName(ValoTheme.BUTTON_DANGER);
deleteButton.setSizeFull();
Button commentButton = new Button("",VaadinIcons.COMMENT);
commentButton.addStyleName(ValoTheme.BUTTON_PRIMARY);
commentButton.setSizeFull();
Button editButton = new Button("", VaadinIcons.EDIT);
editButton.addStyleName(ValoTheme.BUTTON_FRIENDLY);
editButton.setSizeFull();
rowOne.addColumn().withDisplayRules(12,6,4,4).withComponent(deleteButton);
rowOne.addColumn().withDisplayRules(12,6,4,4).withComponent(commentButton);
rowOne.addColumn().withDisplayRules(12,6,4,4).withComponent(editButton);
ResponsiveRow rowTwo = responsiveLayout.addRow();
Label labelOne = new Label("LABEL 1");
Label labelTwo = new Label("LABEL 2");
rowTwo.addColumn().withDisplayRules(12,6,4,4).withComponent(labelOne);
rowTwo.addColumn().withDisplayRules(12,6,4,4).withComponent(labelTwo);
setSizeFull();
addComponent(responsiveLayout);
}
You can view a basic example here
You can combine your layouts, you might want to put two horizontal layouts within a vertical layout. Think of "boxes within boxes". From there you can fine-tune your layout via css, just analyse the generated HTML.
They had a webinar about layouts some time ago, maybe that helps.
来源:https://stackoverflow.com/questions/46342417/vaadin-responsive-columns