使用codenameone中的Toolbar类,如何动态设置SideMenuBar的可见性?

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

当我将命令简单地放在SideMenuBar(METHOD 1)上时,我实现了想要的行为,但是现在我已经切换到使用Toolbar类来发挥LnF的优势(METHOD 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,而不必调用或更改任何主题常量。

将工具栏定为final或在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;
    }
});

10-07 16:46