[英]JavaFX ListView - remove spacing / padding / margin between cells
As I have custom cells for a particular ListView
, I have big trouble to understand how to to remove the "natural" sort of margin / spacing there is between each cells, and, even worse, on the right and left sides of said cells.由于我有特定ListView
自定义单元格,我很难理解如何删除每个单元格之间的“自然”类型的边距/间距,更糟糕的是,在所述单元格的右侧和左侧。
Here's an example:下面是一个例子:
As you can see, I use a color to bring out each cell.如您所见,我使用一种颜色来显示每个单元格。 You cannot see the space at the end of the cells (on their right side), because I need to scroll, but you can trust me, the white space is equal on each side (bigger on the left & right, though...) which is not normal as I have specifically designed these cells so that their width is as large as the ListView
so that there is no need to use horizontal scrolling.您看不到单元格末尾的空间(在它们的右侧),因为我需要滚动,但是您可以相信我,每一侧的空白区域都是相等的(左侧和右侧更大,但...... ) 这不正常,因为我专门设计了这些单元格,使其宽度与ListView
一样大,因此无需使用水平滚动。 My issue kinds of defeat my purpouse...我的问题有点违背我的目的...
Each cell consists of one AnchorPane with the image and the one label.每个单元格由一个带有图像和一个标签的AnchorPane组成。 The AnchorPane
is painted in yellow ( -fx-background-color: yellow;
). AnchorPane
被涂成黄色( -fx-background-color: yellow;
)。
And as you can clearly see, there is these white spaces all around the cells.正如您可以清楚地看到的那样,单元格周围有这些白色空间。
FYI, here I am using JavaFX 8 / 2.2 SDK but I intend to use JFoenix JFXListView
& JFXListCell
.仅供参考,这里我使用的是JavaFX 8 / 2.2 SDK,但我打算使用JFoenix JFXListView
和JFXListCell
。 However, the spacing is even worse using those.但是,使用这些间距会更糟。
Strange point: I also painted the ListView
in green, but such color is nowhere.奇怪的一点:我也把ListView
涂成绿色了,但是这样的颜色没有。 to be seen.被看到。 I guess all the cells (and the empty one) overwrites the ListView
content, so it would make sense not to see its background.我猜所有单元格(和空单元格)都覆盖了ListView
内容,所以不看它的背景是有意义的。 However, this means the cells are somehow "corrupted" since white spaces are "added" all around my cells.然而,这意味着单元格以某种方式“损坏”,因为在我的单元格周围“添加”了空格。
I have tried to set padding to 0 for everything but in vain.我试图将所有内容的填充设置为 0,但徒劳无功。
Finally, in the onUpdateItem
method, I do call the super method and when the cell is not flagged as empty , I set the graphic to the aforementioned AnchorPane otherwise I set it to null, which is clearly consistent to my screenshot.最后,在onUpdateItem
方法中,我确实调用了super 方法,当单元格没有标记为空时,我将图形设置为上述 AnchorPane ,否则将其设置为 null,这与我的屏幕截图明显一致。
Thanks for the help !谢谢您的帮助 !
If you look at the default CSS stylesheet for JavaFX, modena.css , you'll see:如果您查看 JavaFX 的默认 CSS 样式表modena.css ,您将看到:
.list-cell {
-fx-padding: 0.25em 0.583em 0.25em 0.583em; /* 3 7 3 7 */
}
This is where the padding for the ListCell
s is coming from.这就是ListCell
的填充的来源。 To remove this padding simply add your own stylesheet which contains:要删除此填充,只需添加您自己的样式表,其中包含:
.list-cell {
-fx-padding: 0px;
}
Or call setStyle
for each of your ListCell
s:或者为每个ListCell
调用setStyle
:
setStyle("-fx-padding: 0px;");
The second option would best be done by using a custom cell factory .第二种选择最好通过使用自定义细胞工厂来完成。
listView.setCellFactory(v -> new ListCell<>() {
{
setStyle("-fx-padding: 0px");
}
@Override
protected void updateItem(Object item, boolean empty) {
super.updateItem(item, empty);
if (empty || item == null) {
setText(null);
setGraphic(null);
} else {
// your custom display logic
}
}
});
Also, after some quick testing with JFoenix it looks like using the above will also work for JFXListCell
;此外,在使用 JFoenix 进行一些快速测试后,看起来使用上述内容也适用于JFXListCell
; though you have to be careful about overriding updateItem
because JFXListCell
does a lot in its implementation.尽管您必须小心覆盖updateItem
因为JFXListCell
在其实现中做了很多工作。
I set the red background of the cell as default to make sure that the artifacts I see between cells are in fact created by the .list-cell
.我将单元格的红色背景设置为默认值,以确保我在单元格之间看到的工件实际上是由.list-cell
创建的。
No matter how hard I tried, I could not get rid of it, even with the padding 0px
.无论我多么努力,我都无法摆脱它,即使填充0px
。 The red background was still visible every other cell in my case.在我的情况下,每个其他单元格仍然可以看到红色背景。
I suppose, it is a rounding error...我想,这是一个舍入错误......
So, the only thing that worked was using a negative padding to remove any spacing |因此,唯一有效的方法是使用负填充来移除任何间距 | padding |填充 | margin between cells.单元格之间的边距。
.list-cell{
-fx-font-weight: normal;
-fx-text-fill: white;
-fx-padding: -1px;
-fx-background-color: red;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.