简体   繁体   English

使用Vaadin的布局中的组件位置

[英]Components positions in Layout with Vaadin

I have next problem. 我有下一个问题。 I want to create page with title (at top of page) and menu (at center of page). 我想创建带标题的页面(页面顶部)和菜单(页面中心)。 There is my code: 有我的代码:

package org.crabar.views;

import com.vaadin.annotations.Theme;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.ui.*;
import org.crabar.ViewController;

/**
 * Created by Crabar on 01.11.2014.
 */
@Theme("mytheme")
public class StartView extends VerticalLayout implements View {

    public StartView() {
        setSizeFull();
        setMargin(true);
        createTitle();
        createMenu();
    }

    private void createMenu() {
        VerticalLayout menu = new VerticalLayout();
        menu.setSpacing(true);
        menu.setWidth(150, Unit.PIXELS);
        menu.setHeightUndefined();
        addComponent(menu);
        setComponentAlignment(menu, Alignment.MIDDLE_CENTER);
        Button startGameButton = new Button("Start game", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                ViewController.getInstance().changeView(ViewController.GAME_VIEW);
            }
        });
        startGameButton.setWidth(100, Unit.PERCENTAGE);
        menu.addComponent(startGameButton);

        Button achievementsButton = new Button("Achievements", new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                //
            }
        });
        achievementsButton.setEnabled(false);
        achievementsButton.setDescription("The achievements will be soon...");
        achievementsButton.setWidth(100, Unit.PERCENTAGE);
        menu.addComponent(achievementsButton);
    }

    private void createTitle() {
        Label label = new Label("Craberoid 2.0");
        label.setStyleName("title-style");
        addComponent(label);
        label.setWidth(null);
        label.setHeight(null);
        setComponentAlignment(label, Alignment.TOP_CENTER);
    }
}

All fine except menu's position at screen. 除菜单在屏幕上的位置外都很好。 It looks next: 接下来看:

在此输入图像描述

So it not in center of the screen but between center and bottom. 所以它不在屏幕的中心,而是在中心和底部之间。 I spent few hours but didn't find how to set the menu at center and the title at top. 我花了几个小时,但没有找到如何设置中心的菜单和顶部的标题。

Alignment alone is not enough. 仅靠对齐是不够的。 The wrapping vertical layout will keep your two items at 50% height each. 包裹垂直布局将使您的两个项目各自保持50%的高度。 So you have to tell the layout, you allow the content to expand. 所以你必须告诉布局,你允许内容扩展。 You do this by calling setExpandRatio(menu, 1) after adding the menu to the layout. 您可以将菜单添加到布局通过调用setExpandRatio(menu, 1)来执行此操作。 This will tell Vaadin to give the menu 100% within the layout. 这将告诉Vaadin在布局中100%提供菜单。

From https://vaadin.com/api/7.3.3/com/vaadin/ui/AbstractOrderedLayout.html#setExpandRatio%28com.vaadin.ui.Component,%20float%29 来自https://vaadin.com/api/7.3.3/com/vaadin/ui/AbstractOrderedLayout.html#setExpandRatio%28com.vaadin.ui.Component,%20float%29

This method is used to control how excess space in layout is distributed among components. 此方法用于控制布局中多余空间如何在组件之间分配。 Excess space may exist if layout is sized and contained non relatively sized components don't consume all available space. 如果布局大小并且包含非相对大小的组件不占用所有可用空间,则可能存在过多空间。

Example how to distribute 1:3 (33%) for component1 and 2:3 (67%) for component2 : 示例如何为component1分配1:3(33%),为component2分配2:3(67%):

  layout.setExpandRatio(component1, 1); layout.setExpandRatio(component2, 2); 

If no ratios have been set, the excess space is distributed evenly among all components. 如果未设置比率,则多余空间在所有组件之间均匀分布。

Note, that width or height (depending on orientation) needs to be defined for this method to have any effect. 请注意,需要为此方法定义宽度或高度(取决于方向)才能产生任何效果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM