将边距设置为VerticalFieldManager时会发生奇怪的事情

时间:2013-02-16 10:45:35

标签: blackberry

我想生成一个非零边距的VerticalFieldManager,所以我创建一个VerticalFieldManager,然后使用vfm.setMargin(10,10,10,10) ;之后,将一些字段(buttonFieldObjectChoiceField)放入其中。

看起来太简单吧?但奇怪的事情发生了。当我将注意力集中到最后ObjectChoiceField并按空格切换选择时,ObjectChoiceField消失了。

怎么会这样?这是演示代码,任何人都能找到这个bug?

final class HelloWorldScreen extends MainScreen{
  HelloWorldScreen() {
    // just a demo to show the strange thing
    String [] arr = {"a","b"};
    for(int i = 0; i < 10; i++){

        VerticalFieldManager vfm = new VerticalFieldManager(); // Field.USE_ALL_WIDTH
        vfm.setMargin(10, 10, 10, 10);
        ButtonField btn = new ButtonField(String.valueOf(i));
        ObjectChoiceField ch = new ObjectChoiceField(String.valueOf(i), arr);

        vfm.add(btn);
        vfm.add(ch);

        add(vfm);
    }
  }
}

修改:所需UI边距的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:1)

这很奇怪。

为了那些不运行代码的人的利益,发生的事情是,当您单击对象选择字段时,整个屏幕会垂直滚动一点。在每次垂直滚动之后,屏幕将无法一直向下滚动到底部。经过许多此类操作后,最终大部分屏幕的下半部分都无法访问。

我不知道为什么会发生这种情况。(对我来说看起来像黑莓的bug)

我观察到的是,如果您取消对

的呼吁
    vfm.setMargin(10, 10, 10, 10);
问题消失了。

我建议您使用

的解决方法
    vfm.setPadding(10, 10, 10, 10);

代替?

我知道 margin padding 并不是一回事。但是,根据您的完整UI设计(我无法看到),在这种情况下,设置10像素填充可能足以完成您尝试的操作。


更新:根据所需用户界面的屏幕截图,我能够通过此解决方法生成该屏幕截图。同样,不应该这么多工作,但这个额外的代码对我有用:

public class BugScreen extends MainScreen {

   private static final int BG_COLOR = Color.LIGHTGRAY;
   private static final int FG_COLOR = Color.WHITE;
   private static final int BORDER_LINE_COLOR = Color.GRAY;
   private static final int PAD = 10;

   public BugScreen() {
      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);

      getMainManager().setBackground(BackgroundFactory.createSolidBackground(BG_COLOR));
      // this additional call ensures that when scrolling bounces, the area "behind"
      // the normal visible area is ALSO of BG_COLOR
      setBackground(BackgroundFactory.createSolidBackground(BG_COLOR));

      // this will establish a thin gray padding on the screen's left/right sides
      // NOTE: I seem to get drawing artifacts if I try to use this for ALL sides!
      getMainManager().setPadding(0, PAD, 0, PAD);

      String [] arr = {"a","b"};
      for(int i = 0; i < 10; i++){

         VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH) {
            public int getPreferredWidth() {
               return Display.getWidth() - 2 * PAD;
            }
            public void paint(Graphics graphics) {
               int oldColor = graphics.getColor();
               super.paint(graphics);
               graphics.setColor(BORDER_LINE_COLOR);
               // draw the (1-pixel wide) border size you would like.
               graphics.drawRect(0, 0, getPreferredWidth(), getHeight());
               graphics.setColor(oldColor);
            }
         };

         vfm.setBackground(BackgroundFactory.createSolidBackground(FG_COLOR));
         ButtonField btn = new ButtonField(String.valueOf(i));
         ObjectChoiceField ch = new ObjectChoiceField(String.valueOf(i), arr);

         vfm.add(btn);
         vfm.add(ch);

         // add a separator field to get thin gray margin between (vfm) fields         
         add(new MarginField());

         add(vfm);
      }

      // add one last margin field at the bottom
      add(new MarginField());
   }

   private class MarginField extends Field {
      public MarginField() {
         super(Field.USE_ALL_WIDTH);
      }
      public int getPreferredHeight() { return PAD; }
      protected void paint(Graphics g) {
         int oldColor = g.getColor();
         g.setColor(BG_COLOR);
         g.fillRect(0, 0, getWidth(), getPreferredHeight());
         g.setColor(oldColor);
      }
      protected void layout(int width, int height) {
         setExtent(width, getPreferredHeight());               
      }     
   }
}
相关问题