You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vaadin RTL支持:如何在视图中获取当前Direction值?

在Vaadin视图中获取当前Direction值

要获取当前应用的UI方向(Direction),直接调用UI.getCurrent().getDirection()即可,该方法会返回对应的Direction枚举值(RIGHT_TO_LEFT或LEFT_TO_RIGHT)。

基础使用示例

在视图的业务逻辑中直接获取并判断方向:

// 获取当前UI的方向
Direction currentDirection = UI.getCurrent().getDirection();

// 根据方向执行不同逻辑
if (currentDirection == Direction.RIGHT_TO_LEFT) {
    // 针对RTL布局的代码调整
    System.out.println("当前为RTL方向");
} else {
    // 针对LTR布局的代码调整
    System.out.println("当前为LTR方向");
}

组件级方向获取(可选)

如果需要获取某个组件自身设置的方向(若组件未单独设置,会继承父组件/UI的方向),可以调用组件的getElement().getDirection():

Button myButton = new Button("示例按钮");
Direction buttonDirection = myButton.getElement().getDirection();

异步场景注意事项

如果在非UI线程中获取方向,需要通过UI.access()确保在UI上下文执行:

// 异步任务中获取方向
CompletableFuture.runAsync(() -> {
    UI.getCurrent().access(() -> {
        Direction direction = UI.getCurrent().getDirection();
        // 处理方向相关逻辑
    });
});

内容的提问来源于stack exchange,提问作者alexanoid

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:10:20