如何在代号一中动态设置 SideMenuBar 的可见性?

How do I dynamically set the visibility of the SideMenuBar in Codename one?

使用 codenameone 中的工具栏 class,如何动态设置 SideMenuBar 的可见性?

我正在使用 WebBrowser 组件,我只希望在登录后可以访问 SideMenu。

当我只是在 SideMenuBar 上放置命令(方法 1)时,我实现了我想要的行为,但现在我已经切换到使用工具栏 class 以获得 LnF 优势(方法 2) ,似乎没有观察到 hideLeftSideMenuBool 主题常量。

//METHOD 1
//CHANGING THE THEME DYNAMICALLY HIDES THE SIDEMENUBAR WHEN I'VE SIMPLY 
//ADDED COMMANDS LIKE THIS
current.addCommand(new Command("Home") {
    {
      putClientProperty("place", "side");
    }
});

//METHOD 2
//CHANGING THE THEME DYNAMICALLY DOES NOT HIDE THE SIDEMENUBAR WHEN I'VE
//USED toolbar.addComponentToSideMenu TO ADD BUTTONS WITH COMMANDS 
toolbar = new Toolbar();
current.setToolbar(toolbar);
Button home = new Button("Home");
toolbar.addComponentToSideMenu(home, new Command("Home"){

  @Override
  public void actionPerformed(ActionEvent evt) {
    wb.setURL(startURL);
  }
});

...

//I USED THE FOLLOWING CODE TO DYNAMICALLY SET THE THEME AFTER EVALUATING A 
//WebBrowser URI REGARDLESS OF WHICH METHOD WAS USED TO ADD COMMANDS
wb.setBrowserNavigationCallback(new BrowserNavigationCallback() {
  public boolean shouldNavigate(String url) {
    if ((url.indexOf("users/login") != -1)) {
        try {
            //theme_noside.res has hideLeftSideMenuBool set to true
            theme = Resources.openLayered("/theme_noside");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
            UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
            Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
            current.refreshTheme();
        }catch(IOException e){
            Log.p(e.toString());
        }
    }
    else {
        try {
            //theme.res has hideLeftSideMenuBool set to false
            theme = Resources.openLayered("/theme");
            UIManager.getInstance().setThemeProps(theme.getTheme(theme.getThemeResourceNames()[0]));
            UIManager.getInstance().getLookAndFeel().setMenuBarClass(SideMenuBar.class);
            Display.getInstance().setCommandBehavior(Display.COMMAND_BEHAVIOR_SIDE_NAVIGATION);
            current.refreshTheme();
        }catch(IOException e){
            Log.p(e.toString());
        }
    }
    return true;
  }
});

仅使用工具栏 api,您不必调用或更改任何主题常量。

使您的工具栏最终化或在 beforeShow() 方法之外声明它,以便您可以在内部方法 shouldNavigate(String url).

中访问它

您需要做的就是调用 removeAll(),然后重新设置标题并添加您想要的组件。如果工具栏没有命令或标题,则默认隐藏。

wb.setBrowserNavigationCallback(new BrowserNavigationCallback() {
    public boolean shouldNavigate(String url) {
        if ((url.indexOf("users/login") != -1)) {
            toolbar.removeAll();
            toolbar.setTitleComponent(new Label("My Form", "Title"));
            toolbar.getComponentForm().revalidate();
        } else {
            //Do nothing, since I've already add the commands I want earlier
        }
        return true;
    }
});