ControlsFX 通知中的粗体文本部分

3

我想在通知的正文中编写加粗文本。

Notifications.create()
    .title("My Title")
    .text("This is <bold>text</bold>")
    .showInformation();

据我所知,设置通知文本的唯一方法是使用.text("my text")方法。但是我想在其中使用TextFlow来修改文本的某些部分。
我也尝试过使用CSS,但是它只允许我改变通知的整体外观,因为通过CSS设置特定文本显式不被支持。
现在我的问题是:是否有一种方法可以在通知内部以不同的样式呈现文本的不同部分?
1个回答

3

现在已经搞定了。 解决方案是使用.graphic(Node node)。如果你仍然想在左侧显示图片,你需要把所有东西放在一个Pane中并添加一些padding。 我从这里复制了原始图像。

        BorderPane borderPane = new BorderPane();
    borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
    TextFlow textFlow = new TextFlow();
    textFlow.setPadding(new Insets(15, 0, 5, 5));
    Text text1 = new Text("This is ");
    Text text2 = new Text("bold");
    text2.setStyle("-fx-font-weight: bold");
    textFlow.getChildren().addAll(text1, text2);
    borderPane.setRight(textFlow);
    Notifications.create()
            .title("My Title")
            .graphic(borderPane)
            .hideAfter(Duration.seconds(10))
            .show();

重要提示:使用.show()而不是.showInformation/Error/Warning(),因为后者会覆盖BorderPane。
结果: Notification with TextFlow示例

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接