HorizontalLayout.setComponentAlignment(,) 设置居中没有效果
代码?
Button addUserbtn = new Button();
Button delUserbtn = new Button();
Button mdfUserbtn = new Button();
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.addComponents(addUserbtn,delUserbtn,mdfUserbtn);
horizontalLayout.setComponentAlignment(addUserbtn, Alignment.MIDDLE_CENTER);
horizontalLayout.setComponentAlignment(delUserbtn, Alignment.MIDDLE_CENTER);
horizontalLayout.setComponentAlignment(mdfUserbtn, Alignment.MIDDLE_CENTER);
layout.addComponent(horizontalLayout);
没有给horizontalLayout设定宽度,所以宽度就是所有孩子的宽度的总和,所以感觉alignment没起作用。可以试下horizontalLayout.setWidth(“100%”), 或是给一个具体的宽度,比如400px之类的,总之要比孩子的总宽度要大,就会看出效果。
刚试了下,没有效果。
刚又仔细看了一遍代码,你是想把三个按钮都居中显示是吗?试下看这样
horizontalLayout.addComponents(addUserbtn,delUserbtn,mdfUserbtn);
horizontalLayout.setExpandRatio(addUserbtn, 1);
horizontalLayout.setExpandRatio(mdfUserbtn, 1);
horizontalLayout.setComponentAlignment(addUserbtn, Alignment.MIDDLE_RIGHT);
horizontalLayout.setComponentAlignment(delUserbtn, Alignment.MIDDLE_CENTER);
horizontalLayout.setComponentAlignment(mdfUserbtn, Alignment.MIDDLE_LEFT);
每一个孩子都会又一个slot, 每个slot默认大小是一样的。alignment是align 组件在slot里面的位置
谢谢您的帮助。嗯,这个方法没有起作用。
完整的代码+截图
public class ExercisesUI extends UI {
@Override
protected void init(VaadinRequest request) {
Button addUserbtn = new Button("Add");
Button delUserbtn = new Button("Delete ");
Button mdfUserbtn = new Button("Modify");
HorizontalLayout horizontalLayout = new HorizontalLayout();
horizontalLayout.setWidth("100%");
horizontalLayout.addComponents(addUserbtn, delUserbtn, mdfUserbtn);
horizontalLayout.setExpandRatio(addUserbtn, 1);
horizontalLayout.setExpandRatio(mdfUserbtn, 1);
horizontalLayout.setComponentAlignment(addUserbtn, Alignment.MIDDLE_RIGHT);
horizontalLayout.setComponentAlignment(mdfUserbtn, Alignment.MIDDLE_LEFT);
setContent(horizontalLayout);
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = ExercisesUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}

是居中的。谢谢