2010-04-16 62 views

回答

10

什么,你需要做的是:

  • 创建SizebleVFManager(contentManager)作为VerticalFieldManager的扩展
  • 集显示宽度和高度=(显示高度 - 菜单高度)尺寸contentManager
  • 添加contentManager筛选
  • 创建Horizo​​ntalFieldManager(menuManager)
  • 创建BitmapButtonField(菜单按钮),为讯号分配延长ButtonField字段
  • 的锡永
  • 集FieldChangeListeners到menuButtons
  • 添加menuButtons到menuManager
  • 添加menuManager筛选SizebleVFManager的

样品:

class SizebleVFManager extends VerticalFieldManager 
{ 
    int mWidth = 0; 
    int mHeight = 0; 
    public SizebleVFM(int width, int height, long style) { 
     super(style); 
     mWidth = width; 
     mHeight = height; 
    } 

    public SizebleVFM(int width, int height) { 
     mWidth = width; 
     mHeight = height; 
    } 

    public int getPreferredWidth() { 
     return mWidth; 
    } 

    public int getPreferredHeight() { 
     return mHeight; 
    } 

    protected void sublayout(int width, int height) { 
     width = getPreferredWidth(); 
     height = getPreferredHeight(); 
     super.sublayout(width, height); 
     setExtent(width, height); 
    } 
} 

...

SizebleVFManager contentManager = 
    new SizebleVFManager(Display.getWidth(), Display.getHeight(), 
     VERTICAL_SCROLL|VERTICAL_SCROLLBAR); 

参见
sample of BitmapButtonField and Toolbar

PS虽然它的最好使用标准的菜单......

UPDATE

如果要禁用默认菜单功能,取消菜单的keydown:

protected boolean keyDown(int keycode, int time) { 
    if(Keypad.KEY_MENU == Keypad.key(keycode)) 
    { 
     return true; 
    } 
    else 
    return super.keyDown(keycode, time); 
} 

UPDATE

我已经安装了精彩weather application并理解这个样本可能更都与几项改进:

  • 使用CyclicHFManager作为Horizo​​ntalFieldManager的扩展
  • 显示/隐藏menuManager上的菜单按钮,点击

CyclicHFManager是一个管理器,它将持续关注同一个地方并循环运行所有字段。像BlackBerry - Custom centered cyclic HorizontalFieldManager

class CyclicHFManager extends HorizontalFieldManager { 
    int mFocusedFieldIndex = 0; 
    boolean mCyclicTurnedOn = false; 

    public void focusChangeNotify(int arg0) { 
     super.focusChangeNotify(arg0); 
     if (mCyclicTurnedOn) { 
      int focusedFieldIndexNew = getFieldWithFocusIndex(); 
      if (focusedFieldIndexNew != mFocusedFieldIndex) { 
       if (focusedFieldIndexNew - mFocusedFieldIndex > 0) 
        switchField(0, getFieldCount() - 1); 
       else 
        switchField(getFieldCount() - 1, 0); 
      } 
     } 
     else 
     { 
      mFocusedFieldIndex = getFieldWithFocusIndex(); 
     } 
    } 

    private void switchField(int prevIndex, int newIndex) { 
     Field field = getField(prevIndex); 
     delete(field); 
     insert(field, newIndex); 
    } 
} 

alt text http://img109.imageshack.us/img109/6176/toolbarj.jpg

和全代码示例:

abstract class AScreen extends MainScreen { 
    boolean mMenuEnabled = false; 
    SizebleVFManager mContentManager = null; 
    CyclicHFManager mMenuManager = null; 

    public AScreen() { 
     mContentManager = new SizebleVFManager(Display.getWidth(), Display 
       .getHeight(), VERTICAL_SCROLL | VERTICAL_SCROLLBAR); 
     add(mContentManager); 

     // mMenuManager = new CyclicHFManager(Display.getWidth(), 60); 
     mMenuManager = new CyclicHFManager(); 
     mMenuManager.setBorder(BorderFactory.createBevelBorder(new XYEdges(4, 
       0, 0, 0), new XYEdges(Color.DARKBLUE, 0, 0, 0), new XYEdges(
       Color.WHITE, 0, 0, 0))); 
     mMenuManager.setBackground(BackgroundFactory 
       .createLinearGradientBackground(Color.DARKBLUE, Color.DARKBLUE, 
         Color.LIGHTBLUE, Color.LIGHTBLUE)); 

     for (int i = 0; i < 10; i++) { 
      Bitmap nBitmap = new Bitmap(60, 60); 
      Graphics g = new Graphics(nBitmap); 
      g.setColor(Color.DARKBLUE); 
      g.fillRect(0, 0, 60, 60); 
      g.setColor(Color.WHITE); 
      g.drawRect(0, 0, 60, 60); 
      Font f = g.getFont().derive(Font.BOLD, 40); 
      g.setFont(f); 
      String text = String.valueOf(i); 
      g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f 
        .getHeight()) >> 1); 

      Bitmap fBitmap = new Bitmap(60, 60); 
      g = new Graphics(fBitmap); 
      g.setColor(Color.DARKBLUE); 
      g.fillRect(0, 0, 60, 60); 
      g.setColor(Color.GOLD); 
      g.drawRect(0, 0, 60, 60); 
      g.setFont(f); 
      g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f 
        .getHeight()) >> 1); 

      BitmapButtonField button = new BitmapButtonField(nBitmap, fBitmap, 
        fBitmap); 
      button.setCookie(String.valueOf(i)); 
      button.setPadding(new XYEdges(0, 18, 0, 18)); 

      button.setChangeListener(new FieldChangeListener() { 
       public void fieldChanged(Field field, int context) { 
        Dialog.inform("Button # " + (String) field.getCookie()); 
       } 
      }); 

      mMenuManager.add(button); 
     } 
    } 

    protected boolean keyDown(int keycode, int time) { 
     if (Keypad.KEY_MENU == Keypad.key(keycode)) { 
      if (mMenuManager.getManager() != null) { 
       delete(mMenuManager); 
       mMenuManager.mCyclicTurnedOn = false; 
       mContentManager.updateSize(Display.getWidth(), Display 
         .getHeight()); 
      } else { 
       add(mMenuManager); 
       mMenuManager.getField(2).setFocus(); 
       mMenuManager.mCyclicTurnedOn = true; 
       mContentManager.updateSize(Display.getWidth(), Display 
         .getHeight() 
         - mMenuManager.getHeight()); 
      } 
      return true; 
     } else 
      return super.keyDown(keycode, time); 
    } 
} 

class FirstScreen extends AScreen { 

    public FirstScreen() { 
     mContentManager.add(new LabelField("This is a first screen")); 
    } 
} 

public class ToolbarMenuApp extends UiApplication { 

    public ToolbarMenuApp() { 
     pushScreen(new FirstScreen()); 
    } 

    public static void main(String[] args) { 
     (new ToolbarMenuApp()).enterEventDispatcher(); 
    } 

} 
+0

真棒答案,马克斯。再次,您可以用自己的方式发布详细的代码回复。希望我能+5高举这一点。 :) – 2010-04-18 00:19:53

+0

谢谢马克! :) – 2010-04-18 09:05:05

+0

下周我会重新开始这个项目的工作,但是我阅读并且很快尝试看起来很棒!这是非常有益的,谢谢! 我仍然需要做一些调整来做我想做的事情(事实上,和WeatherEye App完全一样),例如菜单始终可见(启用),并且警报正在工作(而不是白色屏幕)。 我会在下周回来,我确信哈哈。 但是,再次感谢! – Dachmt 2010-04-29 03:09:38