1

我正在尝试实现黑莓控件的平面外观,即 objectchoicefield 和 buttonfield。

以下代码似乎无法解决问题。(宽度设置确实有效,但边框设置无效。)

public static ObjectChoiceField GetDropdownList(String label, String[] data)
{
    ObjectChoiceField ocf = new ObjectChoiceField(null, data, 0, Field.FIELD_LEFT);

    ocf.setBorder(BorderFactory.createSimpleBorder(new XYEdges(0,0,0,0)));
    ocf.setMinimalWidth(Display.getWidth()-61);

    return ocf;
}

无论有没有 setBorder 语句,我都会得到相同的外观。基本上我不想要任何 3D 外观或阴影或光泽或圆角。

谢谢

4

1 回答 1

1

这可能无法满足您的所有需求,但您可以尝试查看我为 OS 4.6 和更低版本的设备构建的自定义 ObjectChoiceField。我想添加一个有光泽的 3D 外观,但您可以更改我用来制作更简单、更扁平的外观的自定义代码。 paint()

以我为例,将圆角半径更改为 1,并删除对的调用,super.paint(g)如下所示:

public class CustomChoiceField extends ObjectChoiceField {

   private int _bgWidth = 0;
   private int _bgHeight = 0;
   private int _numChoices = 0;
   private boolean _hasFocus = false;
   private static final int HIGHLIGHT_COLOR = 0xFF185AB5;  // blue-ish
   private static final int RADIUS = 1;    // rounded corner radius in pixels
   private static final int DFLT_PADDING = 20;

   public CustomChoiceField(Object[] choices, int initialIndex) {
      super("", choices, initialIndex);
      _numChoices = choices.length;
   }

   public int getPreferredHeight() {
      return _bgHeight;
   }

   public int getPreferredWidth() {
      return _bgWidth;
   }

   protected void layout(int width, int height) {
      if (_bgWidth == 0 || _bgHeight == 0) {
         if (height <= Display.getHeight()) {
            // probably using custom Manager to specify size
            _bgWidth = width;
            _bgHeight = height;
         } else {
            // use default sizing
            _bgHeight = DFLT_PADDING + getHeightOfChoices();
            for (int i = 0; i < _numChoices; i++) {
               _bgWidth = Math.max(_bgWidth, DFLT_PADDING + getWidthOfChoice(i));
            }
         }
      }

      super.layout(_bgWidth, _bgHeight);
      super.setExtent(_bgWidth, _bgHeight);
   }   

   protected void applyTheme(Graphics arg0, boolean arg1) {
      // do nothing
   }

   protected void drawFocus(Graphics g, boolean on) {
      // do nothing .. handled manually in paint(g)
   }

   protected void onFocus(int direction) {
      _hasFocus = true;
      super.onFocus(direction);
      invalidate();
   }

   protected void onUnfocus() {
      _hasFocus = false;
      super.onUnfocus();
      invalidate();  // required to clear focus
   }

   protected void paint(Graphics g) {
      int oldColor = g.getColor();

      // field color depends on whether we have focus or not
      int bgColor = (_hasFocus) ? HIGHLIGHT_COLOR : Color.BLACK;
      // when the field has focus, we make it a little less transparent
      int alpha = (_hasFocus) ? 0xDD : 0xBB;
      g.setColor(bgColor);
      g.setGlobalAlpha(alpha);
      g.fillRoundRect(0, 0, _bgWidth, _bgHeight, RADIUS, RADIUS);

      // draw a plain white line as a border
      g.setColor(Color.WHITE);
      g.setGlobalAlpha(0xFF);
      g.drawRoundRect(0, 0, _bgWidth, _bgHeight, RADIUS, RADIUS);

      // draw the currently selected choice's text (also in white)
      String text = (String)getChoice(getSelectedIndex());
      int y = (_bgHeight - getFont().getHeight()) / 2;
      g.drawText(text, 0, y, DrawStyle.HCENTER | DrawStyle.TOP, _bgWidth);
      g.setColor(oldColor);
   }
}

你使用CustomChoiceField这样的:

   private ObjectChoiceField[] ocf = new ObjectChoiceField[3];

   public ObjectChoiceScreen() {
      super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
      Object[] choices1 = new Object[] { "one", "two", "three" };
      ocf[0] = new CustomChoiceField(choices1, 0);
      Object[] choices2 = new Object[] { "ichi", "ni", "san" };
      ocf[1] = new CustomChoiceField(choices2, 0);
      Object[] choices3 = new Object[] { "uno", "dos", "tres" };
      ocf[2] = new CustomChoiceField(choices3, 0);
      for (int i = 0; i < ocf.length; i++) {
         ocf[i].setMargin(new XYEdges(10, 10, 10, 10));
      }
      getMainManager().addAll(ocf);

这不是生产代码,因此您需要自己进行测试。例如,它不处理使用 更改选择setChoices()。但是,这是一个开始,你会得到这样的东西:

在此处输入图像描述

您会注意到前两个对象选择字段和底部的焦点区域之间的颜色差异。

我的代码与正常的选择选项具有相同的弹出窗口ObjectChoiceField。因此,您仍然可能会以这种方式获得圆角。就我而言,我不需要改变那种外观和感觉,所以我也不确定你会如何改变它。

于 2013-10-01T08:27:32.177 回答