Vaadin - Responsive Columns

こ雲淡風輕ζ 提交于 2019-11-29 04:54:16

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!