user-interface - 在 BlackBerry 中创建自定义布局

标签 user-interface blackberry layout custom-controls

我想在屏幕的下半部分创建一个 RichTextField,同时在屏幕的上半部分绘制自己的自定义图形。这在黑莓手机中可能吗?它尝试定义 LayoutManager 并尝试将 RichTextField 定位在屏幕底部,但 RichTextField 滚动整个屏幕。这是 LayoutManager() 的代码。这是正确的方法还是有其他方法可以完成我上面提到的操作。

class LayoutManager extends Manager 
{

  public LayoutManager() 
  { 
    //construct a manager with vertical scrolling    
    super(VERTICAL_SCROLL);
  }

  //overwrite the nextFocus method for custom navigation  
  protected int nextFocus(int direction, boolean alt)  
  {
        return super.nextFocus(direction, alt);
  }

  protected void sublayout(int width, int height) 
  {
    Field field;
    //get total number of fields within this manager
    int numberOfFields = getFieldCount();     
    int x = 0;
    int y = 0;
    System.out.println("******** Fields: " + numberOfFields + " W/H: " + width + " / " + height );
    for(int i = 0;i < numberOfFields;i++) {
      field = getField(i);      //get the field
      x = 20;
      y = 80;
      System.out.println("******** X/Y: " + x + " / " + y);
      setPositionChild(field, x, y);  //set the position for the field
      layoutChild(field, width, y);  //lay out the field
    }
    setPosition(0, 80);
    setExtent(width, 80);

  }

  public int getPreferredWidth() 
  {
   return 160;
  }

  public int getPreferredHeight() 
  {
    int height= 0;
    int numberOfFields= getFieldCount();

    for(int i= 0; i < numberOfFields; i++) 
    {
        height += getField(i).getPreferredHeight();
    }
    return 160;
  }
}

最佳答案

更新 - 自定义滚动条

custom scrollbar http://img146.imageshack.us/img146/7775/scroll.png

具有自定义大小限制和滚动功能的VerticalFieldManager:

class SizedVFM extends VerticalFieldManager {
    int mWidth;
    int mHeight;

    public SizedVFM(int width, int height) {
        super(VERTICAL_SCROLL | VERTICAL_SCROLLBAR);
        mWidth = width;
        mHeight = height;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    public int getPreferredWidth() {
        return mWidth;
    }

    protected void sublayout(int maxWidth, int maxHeight) {
        super.sublayout(maxWidth, maxHeight);
        setExtent(getPreferredWidth(), getPreferredHeight());
    }

    protected void paint(Graphics graphics) {
        super.paint(graphics);
        if (getVisibleHeight() < getVirtualHeight()) {
            int y1 = 0, y2 = 0, x1 = 0, x2 = 0;
            int scrollOff = getVerticalScroll();
            if (scrollOff > 0) {
                y1 = scrollOff + 12;
                y2 = scrollOff + 2;
                x1 = getVisibleWidth() - 20;
                x2 = getVisibleWidth() - 2;

                graphics.setColor(Color.DARKRED);
                int[] xPts = new int[] { x1, x2, x1 + 9 };
                int[] yPts = new int[] { y1, y1, y2 };
                graphics.drawFilledPath(xPts, yPts, null, null);
            }
            if (scrollOff < (getVirtualHeight() - getVisibleHeight())) {
                y1 = scrollOff + getVisibleHeight() - 12;
                y2 = scrollOff + getVisibleHeight() - 2;
                x1 = getVisibleWidth() - 20;
                x2 = getVisibleWidth() - 2;
                graphics.setColor(Color.DARKRED);
                int[] xPts = new int[] { x1, x2, x1 + 9 };
                int[] yPts = new int[] { y1, y1, y2 };
                graphics.drawFilledPath(xPts, yPts, null, null);
            }
        }
    }
}

绘画和文本字段:

class HeaderPainting extends SizedVFM {
    BitmapField mBitmapField;
    public HeaderPainting(Bitmap bitmap, int width, int height) {
        super(width, height);
        add(mBitmapField = new BitmapField(bitmap, FOCUSABLE));
    }
}
class FooterText extends SizedVFM {
    ExRichTextField mTextField;
    public FooterText(String text, int width, int height) {
        super(width, height);
        int bgColor = Color.SANDYBROWN;
        int textColor = Color.DARKRED;
        add(mTextField = new ExRichTextField(text, bgColor, textColor));
    }
    class ExRichTextField extends RichTextField {
        int mTextColor;
        int mBgColor;
        public ExRichTextField(String text, int bgColor, int textColor) {
            super(text);
            mTextColor = textColor;
            mBgColor = bgColor;
        }
        protected void paint(Graphics graphics) {
            graphics.clear();
            graphics.setColor(mBgColor);
            graphics.fillRect(0, 0, getWidth(), getHeight());
            graphics.setColor(mTextColor);
            super.paint(graphics);
        }
    }
}

使用示例:

class Scr extends MainScreen {
    HeaderPainting mBitmapField;
    FooterText mTextField;
    public Scr() {
        int width = Display.getWidth();
        int height = Display.getHeight() / 2;
        Bitmap bitmap = customPaint(width, height);
        String text = "Lorem ipsum dolor sit amet, consectetuer "
                + "adipiscing elit, sed diam nonummy nibh euismod "
                + "tincidunt ut laoreet dolore magna aliquam erat "
                + "volutpat. Ut wisi enim ad minim veniam, quis "
                + "nostrud exerci tation ullamcorper suscipit "
                + "lobortis nisl ut aliquip ex ea commodo consequat. "
                + "Duis autem vel eum iriure dolor in hendrerit in "
                + "vulputate velit esse molestie consequat, vel "
                + "illum dolore eu feugiat nulla facilisis at vero "
                + "eros et accumsan et iusto odio dignissim qui "
                + "blandit praesent luptatum zzril delenit augue "
                + "duis dolore te feugait nulla facilisi.";
        add(mBitmapField = new HeaderPainting(bitmap, width, height));
        add(mTextField = new FooterText(text, width, height));
    }
    protected Bitmap customPaint(int width, int height) {
        Bitmap bmp = new Bitmap(width, height);
        Graphics graphics = new Graphics(bmp);
        graphics.setColor(Color.BLUE);
        graphics.fillRect(10, 10, width - 20, height - 20);
        graphics.setColor(Color.RED);
        graphics.fillRect(10, 10, 50, height - 20);
        return bmp;
    }
}

如果您不喜欢 RichTextField 内的焦点,请参阅
Blackberry Java: TextField without the caret?

关于user-interface - 在 BlackBerry 中创建自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1426081/

相关文章:

httpconnection.getResponseCode() 给出 EOF 异常

java - 哪种布局和容器适合制作像 Tiled 这样的 GUI?

model-view-controller - Haxe NME UI : Best practices for MVC application design

user-interface - BlackBerry - 带有 CheckBoxField 的 TreeField?

user-interface - POS UI设计与开发 : what should be included & avoided?

java - 如何在 jFrame 上布局多个面板? ( java )

html - 如果输入聚焦,iOS7 Safari 布局会因方向变化而中断

java - 如何围绕当前位置画一个圆?

blackberry - Editfield 滚动无法在黑莓中到达顶部

java - BlackBerry - 如何在 GoogleMap 的 BrowserField 中引用位于项目的 res/img 文件夹中的图像