[英]Change ListView font size in JavaFX
我想知道如何在 JavaFx 中更改列表視圖項文本字體大小。 每行文本的大小會有所不同。 我嘗試使用細胞因子屬性,但我不知道如何使用它。 有人可以幫我嗎?
一個類似的問題在這里:
如何在 JavaFX 中更改 ListView 中的字體大小?
您也可以使用下面的.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;
}
如何在 JavaFX 中使用外部 css?
如何使用外部 css 文件設置 javafx 應用程序的樣式
如何以編程方式更改文本的大小?:
在 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));
}
}
};
});
在項目類中存儲文本和文本大小
public class SizedText {
private final int textSize;
private final String text;
public SizedText(int textSize, String text) {
this.textSize = textSize;
this.text = text;
}
public int getTextSize() {
return textSize;
}
public String getText() {
return text;
}
}
並使用cellFactory
返回ListCell
s,根據updateItem
方法中的項目數據設置文本大小。
@Override
public void start(Stage primaryStage) {
ListView<SizedText> list = new ListView<>(FXCollections.observableArrayList(
new SizedText(10, "10"),
new SizedText(15, "15"),
new SizedText(20, "20"),
new SizedText(25, "25"),
new SizedText(30, "30"),
new SizedText(35, "35"),
new SizedText(40, "40"),
new SizedText(50, "50"),
new SizedText(60, "60")
));
list.setCellFactory(l -> new ListCell<SizedText>() {
@Override
protected void updateItem(SizedText item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
} else {
setText(item.getText());
setFont(Font.font(item.getTextSize()));
}
}
});
Scene scene = new Scene(list);
primaryStage.setScene(scene);
primaryStage.show();
}
在 javaFx 中,您可以使用 css 來設置不同對象的樣式。 這取決於列表視圖中的對象,但它會像這樣。
ListView<Hyperlink> listview = ..
ObservableList<Hyperlink> lists = listview.getItems();
for(Hyperlink link:lists)
{
link.setStyle("-fx-background-color:#ffffff");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.