在列表视图 javaFX 的单元格之间插入 space
insert space between cells in list view javaFX
我想在列表视图单元格之间创建间隙,但我没有找到任何合适的方法来完成这项任务。
css 或 java 代码中是否有任何方法可以在它们之间创建间隙?
这是我要找的东西:
在此先感谢,
使用 "nested background" 与 css 中的填充相结合 list-cell
。以下给出了接近您的屏幕截图的结果:
列表视图单元格-gap.css:
.list-view {
-fx-padding: 3px ;
-fx-background-color: darkred ;
}
.list-cell {
-fx-padding: 2px ;
-fx-background-color: transparent, -fx-background ;
-fx-background-insets: 0px, 2px ;
}
.list-cell:empty {
-fx-padding: 2px ;
-fx-background-color: transparent ;
-fx-background-insets: 0 ;
}
测试代码:
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ListViewStyleExample extends Application {
@Override
public void start(Stage primaryStage) {
ListView<String> listView = new ListView<>();
IntStream.rangeClosed(1, 20).mapToObj(Integer::toString)
.map("Item "::concat).forEach(listView.getItems()::add);
Scene scene = new Scene(new BorderPane(listView), 250, 400);
scene.getStylesheets().add("list-view-cell-gap.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我想在列表视图单元格之间创建间隙,但我没有找到任何合适的方法来完成这项任务。 css 或 java 代码中是否有任何方法可以在它们之间创建间隙?
这是我要找的东西:
使用 "nested background" 与 css 中的填充相结合 list-cell
。以下给出了接近您的屏幕截图的结果:
列表视图单元格-gap.css:
.list-view {
-fx-padding: 3px ;
-fx-background-color: darkred ;
}
.list-cell {
-fx-padding: 2px ;
-fx-background-color: transparent, -fx-background ;
-fx-background-insets: 0px, 2px ;
}
.list-cell:empty {
-fx-padding: 2px ;
-fx-background-color: transparent ;
-fx-background-insets: 0 ;
}
测试代码:
import java.util.stream.IntStream;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
public class ListViewStyleExample extends Application {
@Override
public void start(Stage primaryStage) {
ListView<String> listView = new ListView<>();
IntStream.rangeClosed(1, 20).mapToObj(Integer::toString)
.map("Item "::concat).forEach(listView.getItems()::add);
Scene scene = new Scene(new BorderPane(listView), 250, 400);
scene.getStylesheets().add("list-view-cell-gap.css");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}