在borderlayout-codenameone中居中放置组件

在borderlayout-codenameone中居中放置组件,codenameone,Codenameone,我使用了borderLayout center,但它只是根据屏幕宽度将组件居中,而不是屏幕高度。所以我使用了BorderLayout.CENTER\u BEHAVIOR\u CENTER。它将组件集中在窗体的中间,但我的动画应该是整个屏幕大小,但它只需要组件大小。< /P> 菜单动画: private void menuAnimation(Container c) { int w = Display.getInstance().getDisplayWidth(); int h =

我使用了borderLayout center,但它只是根据屏幕宽度将组件居中,而不是屏幕高度。所以我使用了BorderLayout.CENTER\u BEHAVIOR\u CENTER。它将组件集中在窗体的中间,但我的动画应该是整个屏幕大小,但它只需要组件大小。< /P> 菜单动画:

private void menuAnimation(Container c) {
    int w = Display.getInstance().getDisplayWidth();
    int h = Display.getInstance().getDisplayHeight();
    int[] positionX = {-100, w / 2, w + 100, w + 100, w + 100, w / 2, -100, -100};
    int[] positionY = {-100, -100, -100, h / 2, h + 100, h + 100, h + 100, h / 2};
    for (int iter = 0; iter < c.getComponentCount(); iter++) {
        Component cmp = c.getComponentAt(iter);
        cmp.setY(positionY[iter % positionY.length]);
        cmp.setX(positionX[iter % positionX.length]);
    }
}

发生这种情况的原因是组件包含在边界布局位置内的容器中。因此,当对其设置动画时,将在其父容器的边界之外进行渲染,父容器将其剪裁到位


一个可能的解决方案可能是在事实发生后为中心布局设置约束,然后使用
animateLayoutHierarchy
,这将为约束效果和元素的位置设置动画

发生这种情况的原因是组件包含在边框布局位置内的容器中。因此,当对其设置动画时,将在其父容器的边界之外进行渲染,父容器将其剪裁到位


一个可能的解决方案可能是在事实发生后为中心布局设置约束,然后使用
animateLayoutHierarchy
,这将为约束效果和元素的位置设置动画

我不理解解决方案部分,抱歉,如果不使用中心约束,UI将在整个屏幕上展开,剪辑将正常工作。当您应用新的布局约束,然后调用animateLayoutHierarchy时,它将为整个层次(而不是特定组件)设置动画,并将慢慢缩小父容器,以便动画应可见。我已删除中心约束。但动画仍然仅在组件内。我还需要将整个组件组放置在屏幕的中心。你能在你的答案中更正我的代码吗?我不能更正代码,因为它非常重要,并且有外部依赖性。我建议在模拟器中打开慢动作模式,并为层次结构中的每个容器设置一个线条边框,以查看容器在哪里剪裁UI。这将允许您查看需要修复的容器,感谢它的帮助。我在容器中添加了填充,使其具有所需的尺寸,但这样做时,我必须以不同的方式编码方向,等等。是否有一种方法可以使组件(相对于宽度和高度)在特定尺寸中居中?我在borderlayout中尝试了约束和行为,它会剪裁多余的区域。我不理解解决方案部分,抱歉,如果不使用中心约束,UI将在整个屏幕上展开,剪裁将正常工作。当您应用新的布局约束,然后调用animateLayoutHierarchy时,它将为整个层次(而不是特定组件)设置动画,并将慢慢缩小父容器,以便动画应可见。我已删除中心约束。但动画仍然仅在组件内。我还需要将整个组件组放置在屏幕的中心。你能在你的答案中更正我的代码吗?我不能更正代码,因为它非常重要,并且有外部依赖性。我建议在模拟器中打开慢动作模式,并为层次结构中的每个容器设置一个线条边框,以查看容器在哪里剪裁UI。这将允许您查看需要修复的容器,感谢它的帮助。我在容器中添加了填充,使其具有所需的尺寸,但这样做时,我必须以不同的方式编码方向,等等。是否有一种方法可以使组件(相对于宽度和高度)在特定尺寸中居中?我在borderlayout中尝试了约束和行为,它会剪裁多余的区域。
    //f.setLayout(new FlowLayout(Component.CENTER, Component.CENTER));
    f.setLayout(new BorderLayout(BorderLayout.CENTER_BEHAVIOR_CENTER));

    Container menuContainerGroup = new Container(new BoxLayout(BoxLayout.Y_AXIS));
    f.add(menuContainerGroup);

    TableLayout tl = new TableLayout(3, 3);
    Container menuContainer = new Container(tl);

    menuContainerGroup.add(menuContainer);

    Image round = theme.getImage("loginBg.png").scaledWidth(imgWidth / 3 - 10);

    Label menuIcon = new Label();
    menuIcon.setUIID("menuButton");

    Button menuIcon1 = new Button(round);
    menuIcon1.setUIID("menuButton");
    menuIcon1.addActionListener((e) -> {
        menuAnimation(menuContainer);
        menuContainer.animateUnlayoutAndWait(600, 20);
        showForm("", null);

    });

    Label menuIcon2 = new Label();
    menuIcon2.setUIID("menuButton");

    Button menuIcon3 = new Button(round);
    menuIcon3.setUIID("menuButton");
    menuIcon3.addActionListener((e) -> {
        menuAnimation(menuContainer);
        menuContainer.animateUnlayoutAndWait(600, 20);
        showForm("", null);
    });

    Button menuIcon4 = new Button("Sign Out");
    menuIcon4.setUIID("menuButton");
    menuIcon4.getAllStyles().setFgColor(0xff7800);
    menuIcon4.getAllStyles().setAlignment(Component.CENTER);
    menuIcon4.addActionListener((e) -> {
        menuAnimation(menuContainer);
        menuContainer.animateUnlayoutAndWait(600, 20);
        showForm("", null);
    });

    Button menuIcon5 = new Button(round);
    menuIcon5.setUIID("menuButton");
    menuIcon5.addActionListener((e) -> {
        menuAnimation(menuContainer);
        menuContainer.animateUnlayoutAndWait(600, 20);
        showForm("", null);
    });

    Label menuIcon6 = new Label();
    menuIcon6.setUIID("menuButton");

    Button menuIcon7 = new Button(round);
    menuIcon7.setUIID("menuButton");
    menuIcon7.addActionListener((e) -> {
        menuAnimation(menuContainer);
        menuContainer.animateUnlayoutAndWait(600, 20);
        showForm("", null);
    });

    Label menuIcon8 = new Label();
    menuIcon8.setUIID("menuButton");

    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon1);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon2);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon3);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon4);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon5);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon6);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon7);
    menuContainer.add(tl.createConstraint().widthPercentage(33), menuIcon8);
    f.revalidate();