繁体   English   中英

如何制作一个ListView <ImageView> 占用的空间不会超过所需的空间吗?

[英]How to make a ListView<ImageView> not take more space than it needs?

我用一些图像做了一个ListView<ImageView>

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class MyApp extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Image img = new Image("main/img.png");
        ObservableList<ImageView> list = FXCollections.observableArrayList();
        for (int i = 0; i < 4; i++) {
            ImageView iView = new ImageView(img);
            list.add(iView);
        }

        ListView<ImageView> listView = new ListView<>(list);
        listView.setOrientation(Orientation.HORIZONTAL);

        Group root = new Group(listView);
        Scene scene = new Scene(root, 500, 500);
        stage.setScene(scene);
        stage.sizeToScene();
        stage.show();
    }

    public static void main(String[] args) throws Exception {

        launch(args);
    }
}

在此处输入图片说明

但这是它的样子

在此处输入图片说明

我希望图像完全覆盖每个单元-没有插图,没有填充,没有滚动条。 它们之间只是一个边界。

我想绑定列表视图的heightProperty ,但是它是只读的。 图像视图也没有大小属性​​,但我认为是占据空间的是单元格而不是图像视图。

我还尝试添加这样的Pane

Image img = new Image("main/img.png");
ObservableList<Pane> list = FXCollections.observableArrayList();
for (int i = 0; i < 4; i++) {
    ImageView iView = new ImageView(img);
    Pane pane = new BorderPane(iView);
    pane.setStyle("-fx-background-color: yellow;");
    list.add(pane);
}

ListView<Pane> listView = new ListView<>(list);
listView.setOrientation(Orientation.HORIZONTAL);

这给出了这个结果

在此处输入图片说明

还有一个渲染器

list.setCellFactory((ListView<ImageView> l) -> new FitCell());

但您也不能绑定单元格的属性。 如何在不手动设置高度和宽度的情况下执行此操作?

听起来似乎根本就不需要ListView ImageView放在HBox ,然后将HBox包装在ScrollPane 您可以使滚动窗格成为可移动窗格,并根据需要设置滚动条策略。 这使您可以完全控制布局等,而不必担心虚拟单元的复杂性。

例如

HBox hbox = new HBox();
Image img = new Image("main/img.png");
for (int i = 0; i < 4; i++) {
    ImageView iView = new ImageView(img);
    Pane pane = new StackPane(iView);
    pane.setStyle("-fx-background-color: yellow;");
    hbox.getChildren().add(pane);
}

ScrollPane scroller = new ScrollPane(hbox);
scroller.setPannable(true);
scroller.setFitToHeight(true);
scroller.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

Group root = new Group(scroller);

// ...

您可能需要在窗格上设置其他样式,例如,填充和边框等,但这应足以为您提供所需的样式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM