【问题标题】:Strange things happen when set margin to VerticalFieldManager将边距设置为 VerticalFieldManager 时会发生奇怪的事情
【发布时间】:2013-02-16 10:45:34
【问题描述】:

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

这似乎太简单了,对吧?但奇怪的事情发生了。当我关注最后一个ObjectChoiceField 并按空格键切换选择时,ObjectChoiceField 消失了。

怎么可能?这是demo代码,哪位大神帮忙找找?

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 边距的屏幕截图:

【问题讨论】:

  • +1 一个小的,独立的例子,重现你的问题:)

标签: blackberry


【解决方案1】:

这很奇怪。

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

我不知道为什么会这样。(在我看来,这就像一个 BlackBerry 错误)

我观察到的是,如果您拨打电话

    vfm.setMargin(10, 10, 10, 10);

问题消失了。

我可以建议您使用的解决方法

    vfm.setPadding(10, 10, 10, 10);

改为?

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


更新:根据您所需 UI 的屏幕截图,我能够使用此解决方法生成它。同样,不应该做这么多工作,但是这个额外的代码对我有用:

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());               
      }     
   }
}

【讨论】:

  • 嗨,Nate,您能尝试将问题描述得如此详细,真是太好了。非常感谢!
  • 嗨,Nate,您能尝试将问题描述得如此详细,真是太好了。非常感谢! (抱歉,我不知道如何开始新的一行)好吧,我使用该代码只是因为我想制作这样的 UI,pocketberry.com/wp-content/uploads/2011/08/…。你能看到“功能”和“存储”有狭窄和灰色的图案吗?起初我认为这很简单,只需使用一个垂直字段管理器并为其设置边距。但结果我错了。也许有人有更好的想法来做到这一点?
  • 谢谢,我试过你的解决方案,效果很好。基于此,我想到了一个新的想法,下面的代码可以为我工作,看起来有点简单:
  • for(int i = 0; i
猜你喜欢
  • 1970-01-01
  • 2013-02-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 2011-11-25
  • 1970-01-01
相关资源
最近更新 更多