vaadin - 如何居中对齐 Vaadin TextField 中的文本
问题描述
我阅读了 CSS 样式部分(https://vaadin.com/docs/v14/flow/styling/styling-components),它提到全局 CSS 不会影响影子 DOM 中的“输入”字段,所以样式必须是添加到 shdaow DOM,但不幸的是,它没有明确说明如何将样式添加到影子 DOM。注意。我主要使用 Flow 纯 java 和一点 CSS。
我尝试从组件中检索元素,然后检索 shadowRoot,然后从根中检索“输入”元素以向其添加样式,不幸的是,这不起作用,shadowroot 为空(此代码从视图类中的 onAttach() 方法执行)
private void setTextAlignCenterForTextFields(TextField textField) {
//find the internal 'Input' and set the styling to text-align=center, unfortunately
// you cant do that with global css, since the 'input' element is in shadow root
textField.getElement()
.getShadowRoot()
.get()
.getChildren()
.filter( elem -> "INPUT".equalsIgnoreCase(elem.getTag()))
.forEach(inputElem -> inputElem.getStyle().set("text-align", "center"));
}
任何想法,将不胜感激。我正在使用 Vaadin 版本 14.5.1。
解决方案
已经有一个主题变体来对齐文本
centerTextField.addThemeVariants(TextFieldVariant.LUMO_ALIGN_CENTER);
见https://vaadin.com/components/vaadin-text-field/java-examples/theme-variants
至于如何将 CSS 附加到阴影根,基本上使用themeFor
,请参阅https://vaadin.com/docs/v14/flow/styling/importing-style-sheets/#component-styles
推荐阅读
- validation - 接口转换:interface {} 为 nil,不是 validator.ValidationErrors
- node.js - 路由器无法连接到 MongoDB 集群
- swift - 推送通知不会在 ios 中自动触发
- oracle - PL/SQL 函数 - 批量收集到管道行
- azure-devops - 如何在 Azure DevOps 上的发布管道中添加插槽交换任务之前的批准
- java - 过滤给定集合元素的增量条件
- ionic4 - 如何动态转到上一页(离子4)?
- javascript - 为什么为破坏变量未定义
- docker - 进程一直挂着
- typescript - 如何选择对象的字符串成员