gpt4 book ai didi

android - 代号 One BoxLayout 滚动

转载 作者:行者123 更新时间:2023-11-29 01:25:40 25 4
gpt4 key购买 nike

我目前正在用代号一写一个简单的游戏,我希望用户选择国家。

我希望在可滚动列表中将国家表示为图像。

我有一个在 X 轴上带有 BoxLayout 的容器,以及内部带有图标的按钮。

问题是不适合容器的列表项正在被裁剪并且没有在应该绘制的时候绘制。此外,当手指位于未绘制项目上方时,容器不可滚动。

Here is the list

And here how it looks like after scroll

这是来源:

    final Container cont_nations = new Container(new BoxLayout(BoxLayout.X_AXIS));

int buttons_width = dim_nation_man.getWidth() * 5;
cont_nations.setPreferredW(buttons_width + 50);
cont_nations.setPreferredH(dim_nation_man.getHeight());
cont_nations.setX(Main.screenWidthHalf - buttons_width / 2);
cont_nations.setY(choiseImage.getY() + dim_choise.getHeight() + Main.cellSize * 0);
cont_nations.setScrollSize(new Dimension(man_size * imgName_nations.length, man_size));
cont_nations.setScrollable(true);
//cont_nations.setScrollVisible(false);

addComponent(cont_nations);

for (int i = 0; i < img_nations.length; i++) {
final Button btn_nation = new Button(img_nations[i][0].image);

cont_nations.addComponent(btn_nation);
}

我知道我做错了。也许我不应该使用Container,或者我应该以另一种方式用 child 填充它,或者为 child 使用另一个类。但我找不到任何信息出了什么问题。

编辑

我尝试了 Shai Almog 的建议,仅使用 setScrollableX(),但这并没有按照我的需要工作。容器移至左上角并且不可滚动。

首先,我需要此滚动位于特定位置并具有特定大小,因此我使用 CoordinationLayout 作为容器的父级。这就是为什么我将 setX()、setY() 和 setPreferredW() 与 setPreferredH() 一起使用。

最佳答案

您只想在 X 轴上滚动吗?

删除所有这些代码:

int buttons_width = dim_nation_man.getWidth() * 5;
cont_nations.setPreferredW(buttons_width + 50);
cont_nations.setPreferredH(dim_nation_man.getHeight());
cont_nations.setX(Main.screenWidthHalf - buttons_width / 2);
cont_nations.setY(choiseImage.getY() + dim_choise.getHeight() + Main.cellSize * 0);
cont_nations.setScrollSize(new Dimension(man_size * imgName_nations.length, man_size));
cont_nations.setScrollable(true);

按钮将根据其图标大小调整大小,因此您无需触摸它。设置位置将不起作用,因为布局管理器将确定这一点。

只需使用:

cont_nations.setScrollableX(true);

注意最后的X...

由于您的评论,为了完整起见,我添加了一个完整的工作示例来证明这一点:

    Form hi = new Form("Side Scroll");
hi.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
int[] colors = { 0xffff0000, 0xff00ff00, 0xff0000ff, 0xff000000 };
Container side = new Container(new BoxLayout(BoxLayout.X_AXIS));
side.setScrollableX(true);
hi.addComponent(side);
for(int iter = 0 ; iter < 30 ; iter++) {
side.addComponent(new Button(Image.createImage(100, 50, colors[iter % colors.length])));
}
hi.show();

enter image description here

关于android - 代号 One BoxLayout 滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34056010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com