据我所知,一个ScrollPane可以通过使用一个表作为ScrollPane小部件来添加多个小部件(这是在libGDX回购中的ScrollPane测试中所做的)。
我希望实现类似的目标,但希望在ScrollPane小部件中对许多参与者进行绝对定位,而不是使用一个表作为ScrollPane小部件来提供表格定位。
最后,我得到了这段不工作的代码,但是根据libGDX javadoc,它应该会工作,我不知道它为什么不能工作!
stage = getStage();
// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Game.getWidth(), Game.getHeight());
// Scroll pane inner container
WidgetGroup widgetGroup = new WidgetGroup();
widgetGroup.setFillParent(true);
widgetGroup.addActor(new Image(MAP_TEXTURE_ATLAS.findRegion("map")));
// Scroll pane
ScrollPane scrollPane = new ScrollPane(widgetGroup);
container.setActor(scrollPane);
stage.addActor(container);屏幕上什么也没有显示;
但是,它确实可以使用下面的代码(这显然不是我想要的,因为ScrollPane小部件是一个容器,只能有一个参与者)
// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());
// Scroll pane inner container
Container container2 = new Container();
container2.setBackground(new TextureRegionDrawable(MAP_TEXTURE_ATLAS.findRegion("map")));
// Scroll pane
ScrollPane scrollPane = new ScrollPane(container2);
container.setActor(scrollPane);
stage.addActor(container);是否有一种将WidgetGroup与ScrollPane结合使用的方法,或如何实现所需功能的任何方法。
谢谢
接受答案的替代实现
在阅读了被接受的答案后,我决定创建自己的类,实现如下;
// Scroll pane outer container
Container<ScrollPane> container = new Container<ScrollPane>();
container.setSize(Match3.getWidth(), Match3.getHeight());
class ScrollWidget extends WidgetGroup {
private float prefHeight;
private float prefWidth;
public ScrollWidget(Image image) {
prefHeight = image.getHeight();
prefWidth = image.getWidth();
addActor(image);
}
@Override
public float getPrefHeight() {
return prefHeight;
}
@Override
public float getPrefWidth() {
return prefWidth;
}
}
ScrollWidget scrollWidget = new ScrollWidget(
new Image(MAP_TEXTURE_ATLAS.findRegion("map"))
);
// Scroll pane
ScrollPane scrollPane = new ScrollPane(scrollWidget);
scrollPane.setOverscroll(false, false);
scrollPane.layout();
scrollPane.updateVisualScroll();
scrollPane.setScrollY(scrollPane.getMaxY());
container.setActor(scrollPane);
stage.addActor(container);发布于 2016-03-30 08:18:50
在使用WidgetGroup时,您必须自己设置大小。图像演员不知道什么是首选的宽度/高度,并将默认为0。
未经测试的代码,但我自己也使用了类似的代码:
Stage stage = new Stage();
WidgetGroup group = new WidgetGroup();
Scrollpane scrollPane = new ScrollPane(group);
scrollpane.setBounds(0,0,screenWidth,screenHeight);
group.setBounds(0,0,totalWidth,totalHeight);
Image image = new Image(texture);
image.setBounds(0,0,imageWidth,imageHeight);
group.addActor(image);
stage.addActor(scrollPane);https://stackoverflow.com/questions/36288986
复制相似问题