我想知道如何更改 JavaFx 中的 ListView 项文本字体大小。每行文本的大小会有所不同。我尝试使用细胞因子属性,但我不知道如何使用它。有人可以帮我吗?
最佳答案
A similar question is here:
How to change the font size in ListView in JavaFX?
您也可以使用下面的 .css
:
.list-cell:filled:selected:focused, .list-cell:filled:selected {
-fx-background-color: linear-gradient(#328BDB 0.0%, #207BCF 25.0%, #1973C9 75.0%, #0A65BF 100.0%);
-fx-text-fill: white;
}
.list-cell{
-fx-font-size:15.0;
}
.list-cell:even { /* <=== changed to even */
-fx-background-color: white;
}
.list-cell:filled:hover {
-fx-background-color: #0093ff;
-fx-text-fill: white;
}
How to use external css in JavaFX?
How to use external css file to style a javafx application
How to programmatically change the size of text?:
Java8之前:
listView.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
@Override
public ListCell<String> call(ListView<String> p) {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
// decide to add a new styleClass
// getStyleClass().add("costume style");
// decide the new font size
setFont(Font.font(16));
}
}
};
}
});
使用 lambda 表达式:
listView.setCellFactory(cell -> {
return new ListCell<String>() {
@Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item);
// decide to add a new styleClass
// getStyleClass().add("costume style");
// decide the new font size
setFont(Font.font(16));
}
}
};
});
关于java - 在 JavaFX 中更改 ListView 字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39620548/