代号一 - 为什么我的 TableLayout 在从另一个 Form 返回时被修改?

Codename one - why is my TableLayout modified when going back from another Form?

这是我的问题:我有一个包含多个选项卡的表单。在第一个中,我有一个包含信息的 Table 。我的问题是,当我打开一个新窗体(比方说,一个对话框)然后关闭它时,我的 Table 布局被意外修改了。如果我打开一个新表单,然后使用后退按钮,会观察到相同的行为。

我在下面设置了一个最小的例子,只有一个选项卡,它说明了问题:

public class spmonMobileApp {
    private Form current;
    private Resources theme;   
    private Table table;

    public void init(Object context) {
        theme = UIManager.initFirstTheme("/theme");

        // Enable Toolbar on all Forms by default
        Toolbar.setGlobalToolbar(true);        
    }

    public void start() {           
        Form tabsForm = new Form("", new LayeredLayout());
        Tabs tabs = new Tabs(Component.BOTTOM);
        Container mainContainer = new Container(new BoxLayout(BoxLayout.Y_AXIS));
        TableModel tableModel = new DefaultTableModel(new String[] {""}, new Object[][] {{"MCR"}, {"SSR"}, {"HQ"}}, false);

        table = new Table(tableModel) {
            @Override
            protected Component createCell(Object value, int row, int column, boolean editable) { // (1)
                Component p = new Label((String)value);

                p.getAllStyles().setBgColor(0xFFFF00);
                p.getAllStyles().setBgTransparency(255);
                p.getAllStyles().setAlignment(CENTER);

                p.addPointerPressedListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent evt) {
                        //createDataForm((String)value).show();
                        System.out.println(((TableLayout)table.getLayout()).isGrowHorizontally());
                        Dialog.show("Example", "Just a test", "OK", "Cancel");
                        System.out.println(((TableLayout)table.getLayout()).isGrowHorizontally());
                    }
                }); 

                return p;
            }
        };

        ((TableLayout)table.getLayout()).setGrowHorizontally(true);
        mainContainer.add(table);

        tabs.addTab("Welcome", mainContainer);
        tabsForm.add(tabs);

        tabsForm.show();
    }

    public void stop() {
        current = Display.getInstance().getCurrent();
        if(current instanceof Dialog) {
            ((Dialog)current).dispose();
            current = Display.getInstance().getCurrent();
        }
    }

    public void destroy() {
    }

}

特别是,可以看出 isGrowHorizo​​ntally() 在打开对话框之前为真,之后为假(因此标签被缩小)。

视觉效果如下图所示(左边是打开对话框之前(期望的行为),右边是关闭对话框之后):

example

所以,有谁知道它为什么这样做?如果是这样,有谁知道如何解决这个问题(或者我做错了什么 :-))?

如果不是,有谁知道如何使单元格跨越屏幕的整个宽度并在对话框关闭后仍保持这种状态?

提前致谢!

天津四

问题是您依赖 Table 保持相同的布局实例,但事实并非如此。每当刷新 table 时,布局实例就会被替换。

"right" 方法是为 table 单元格定义约束,例如:

table = new Table(tableModel) {
   protected TableLayout.Constraint createCellConstraint(Object value, int row, int column) {
       TableLayout.Constraint c = super.createCellConstraint(value, row, column);
       c.setWidthPercentage(-2);
       return c;
   }


  ... rest of your code
};

-2 是一种特殊情况,它为列提供了全宽。在这种情况下,我将它应用于所有单元格,但您可能应该只将它应用于一列,因此您应该调整该语句。