Listview 在列表视图javaFX中的单元格之间插入空格

Listview 在列表视图javaFX中的单元格之间插入空格,listview,javafx,Listview,Javafx,我想在listview单元格之间创建间隙,但我没有找到任何合适的方法来执行此任务。 css或java代码中是否有任何方法可以在它们之间创建间隙 以下是我想要的: 感谢您的改进,在css中为列表单元格使用“嵌套背景”和填充。下面给出的结果与您的屏幕截图非常接近: list-view-cell-gap.css: .list-view { -fx-padding: 3px ; -fx-background-color: darkred ; } .list-cell { -f

我想在listview单元格之间创建间隙,但我没有找到任何合适的方法来执行此任务。 css或java代码中是否有任何方法可以在它们之间创建间隙

以下是我想要的:

感谢您的改进,

在css中为列表单元格使用“嵌套背景”和填充。下面给出的结果与您的屏幕截图非常接近:

list-view-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);
    }
}
import java.util.stream.IntStream;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类ListViewStyleExample扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
ListView ListView=新建ListView();
IntStream.rangeClosed(1,20).mapToObj(整数::toString)
.map(“项”::concat).forEach(listView.getItems()::添加);
场景=新场景(新边框窗格(listView),250400);
scene.getStylesheets().add(“list-view-cell-gap.css”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}
在css中为
列表单元格
使用“嵌套背景”和填充。下面给出的结果与您的屏幕截图非常接近:

list-view-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);
    }
}
import java.util.stream.IntStream;
导入javafx.application.application;
导入javafx.scene.scene;
导入javafx.scene.control.ListView;
导入javafx.scene.layout.BorderPane;
导入javafx.stage.stage;
公共类ListViewStyleExample扩展了应用程序{
@凌驾
公共无效开始(阶段primaryStage){
ListView ListView=新建ListView();
IntStream.rangeClosed(1,20).mapToObj(整数::toString)
.map(“项”::concat).forEach(listView.getItems()::添加);
场景=新场景(新边框窗格(listView),250400);
scene.getStylesheets().add(“list-view-cell-gap.css”);
初级阶段。场景(场景);
primaryStage.show();
}
公共静态void main(字符串[]args){
发射(args);
}
}

是否要在单元格之间或文本与单元格边界之间创建间隙?@ItachiUchiha在单元格之间创建间隙,但在IMHO中,这种情况很奇怪。ListView单元格彼此相邻放置。你应该看看我刚才增加了文本和边框之间的空间。这是你的电话号码。如果没有,请添加一个图像以支持您正在寻找的内容。@ItachiUchiha我已经更新了我的问题您想在单元格之间创建间隙还是在文本和单元格边界之间创建间隙?@ItachiUchiha在单元格之间创建间隙,但在IMHO中,这是奇怪的。ListView单元格彼此相邻放置。你应该看看我刚才增加了文本和边框之间的空间。这是你的电话号码。如果没有,请添加一张图片来支持您所寻找的内容。@ItachiUchiha我已经更新了我的问题,应该调整哪些值来增加或减少边距?我需要一些有形的利润,而玩弄你的方法似乎并没有增加利润。应该调整哪些值来增加或减少利润?我需要一些有形的利润,而玩弄你的方法似乎并没有增加利润