【问题标题】:Codename One BoxLayout scroll代号一BoxLayout滚动
【发布时间】:2016-03-07 11:27:39
【问题描述】:

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

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

我有一个 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,或者我应该用另一种方式填充孩子,或者为孩子使用另一个类。 但是我找不到任何信息是什么问题。

编辑

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

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

【问题讨论】:

    标签: android ios codenameone


    【解决方案1】:

    您只想在 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...

    由于您的 cmets 的完整性,我添加了一个完整的工作示例来证明这一点:

        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();
    

    【讨论】:

    • 我现在无法检查,所以我稍后会检查,但我记得,那没有用。容器没有滚动,因为它的大小取决于它的子项。
    • 图片应该占据空间,滚动只会在需要时触发。这是它应该工作的方式,所以从那开始并继续构建。
    • 我试过你的建议,但没有奏效。请参阅我对问题的编辑。
    • 查看完整样本的修订答案。你在别处做错了什么。
    • 我试过你的例子,它有效。看起来我发现了问题。我有一个表单,它有一个带有 CoordinateLayout 和表单大小的容器,并且该容器中有一个可滚动的容器。所以它是这样的:Form -> Container -> Scrollable Container。我使用 2 Containers 的原因是我需要一个很少变化的静态背景,而其他视图随着动画的变化而变化。
    【解决方案2】:

    我对 BorderLayout 和 CoordinateLayout 源代码进行了一些调查,但我找不到任何问题。 他们都正确地为孩子调用了 setWidth() 和 setHeight() 方法。

    但是,我注意到,如果我将 Container 放入 BorderLayout 中,滚动会根据需要进行,因此我想到了用我的 Scrollable Container 创建一个 BorderLayout 容器并将其放置在应有的位置。

    我还是觉得 CoordinateLayout 有 bug,总有一天我会去找的。​​p>

    现在代码看起来像这样,它可以工作了:

    final Container borderContainer = new Container(new BorderLayout());
    int buttons_width = dim_nation_man.getWidth() * 5;
    borderContainer.setX(Main.screenWidthHalf - buttons_width / 2);
    borderContainer.setY(choiseImage.getY() + dim_choise.getHeight() + Main.cellSize * 0);
    borderContainer.setPreferredW(buttons_width);
    borderContainer.setPreferredH(dim_nation_man.getHeight());
    
    addComponent(borderContainer);
    
    final Container cont_nations = new Container(new BoxLayout(BoxLayout.X_AXIS));
    
    cont_nations.setScrollableX(true);
    cont_nations.setScrollVisible(false);
    
    borderContainer.addComponent(BorderLayout.CENTER, 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-30
      • 2020-10-13
      • 2018-01-07
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多