Java/Vaadin - 对各个视图使用不同的主题

Java/Vaadin - Using different theme for individual Views

简短版本:Vaadin 是否支持在 运行 应用程序中为不同的 views/layouts/components 使用不同的主题?

更长的版本:我正在尝试实现一些非常基本的功能/"modularity",其中提供了 class 扩展视图,然后将其添加到特定容器。目前 "ok" 有效,使用此处 中看到的方法来查找特定的 class。但是这个视图现在将使用我的应用程序的主 UI 中使用的主题。有没有什么方法可以让这个视图(或者它可能是什么,例如 CustomComponent)使用另一个主题?

据我所知,主题在运行时是可以切换的(参见下面的示例代码),但它们设置在 global/UI 级别。如果视图完全改变并且您没有任何不能改变的公共部分(例如菜单栏),这可能适合您。

public class MyUI extends UI {
    @Override
    protected void init(VaadinRequest request) {
        VerticalLayout content = new VerticalLayout();
        content.setSizeFull();
        setContent(content);

        content.addComponent(new TextField());
        content.addComponent(new Button("Switch theme", e -> {
            if (getTheme().equals("mytheme1")) {
                setTheme("mytheme2");
            } else {
                setTheme("mytheme1");
            }
        }));
    }
}

否则,主题引擎允许您为各种组件 define your own styles(甚至覆盖默认组件)并使用 addStyleName():

应用它们

Java class:

myButton.addStyleName("red-border");

主题配置:

@mixin mytheme1 {
  @include valo;
  // Insert your own theme rules here

  .red-border {
    // custom style added with addStyleName()
    border: 1px red solid;
  }

  .v-button {
    // global override of default v-button rule
    background-color: green;
  }
}