如何为所有黑莓版本创建哪些套件的CustomLabelField?

时间:2009-10-23 11:06:42

标签: blackberry

我创建了自己的customlabelfield,使用这个customlabelfield我可以修改背景颜色,字体颜色,宽度和高度...当我在版本4.6中使用此标签字段时,我可以得到预期的输出,如果单行中的文本不是套件然后自动进入第二行但是如果我在版本4.5中使用相同的文本则不会到达下一行,而是有时它显示文本的一半,有时甚至不显示单行。如果你有任何想法解决这个问题与我分享。

1 个答案:

答案 0 :(得分:2)

我的按钮控制实现将:

  • 具有固定的大小,字体和文字偏移量
  • 如果标签不能适合单行,则在最多宽度的几行中显示标签
  • 如果没有更多的行可以适合,则在行的末尾显示省略号

alt text http://img297.imageshack.us/img297/8360/multilinebuttons.jpg
自定义按钮代码:

class CustomButton extends ButtonField {
 int mHeight;
 int mWidth;
 int LEFT_OFFSET = 2;
 int TOP_OFFSET = 2;

 public CustomButton(int height, int width, String label) {
  super(label, CONSUME_CLICK);
  mHeight = height;
  mWidth = width;
  setFont(getFont().derive(Font.PLAIN, 16));
 }

 public int getPreferredHeight() {
  return mHeight;
 }

 public int getPreferredWidth() {
  return mWidth;
 }

 protected void layout(int width, int height) {
  super.layout(mWidth, mHeight);
  setExtent(mWidth, mHeight);
 }

 protected void paint(Graphics graphics) {
  int textHeight = getFont().getHeight();
  int twoLinesHeight = 2 * textHeight + TOP_OFFSET;
  // check if first line fit in button height
  int fitHeight = mHeight - 2 * TOP_OFFSET;
  if (textHeight <= fitHeight) {
   graphics.setColor(Color.WHITE);
   String label = getLabel();
   int textLenght = getFont().getAdvance(label);
   // check if whole label fit in button width
   int fitWidth = mWidth - 2 * LEFT_OFFSET;
   if (textLenght <= fitWidth) {
    graphics.drawText(label, LEFT_OFFSET, TOP_OFFSET);
   } else {
    Vector lines = splitLabelToLines();
    int lineTopOffset = TOP_OFFSET;
    int linesCount = lines.size();
    for (int i = 0; i < linesCount; i++) {
     String line = (String) lines.elementAt(i);

     // if lines will not fit in button height, draw ellipsis
     int moreLinesHeight = lineTopOffset + twoLinesHeight;
     boolean moreLinesFit = moreLinesHeight <= fitHeight;
     boolean lastLine = (i == linesCount - 1);
     if (moreLinesFit || lastLine) {
      graphics.drawText(line, LEFT_OFFSET, lineTopOffset);
      lineTopOffset += TOP_OFFSET + textHeight;
     } else {
      line += "...";
      int lineLenght = getFont().getAdvance(line);
      if (lineLenght > fitWidth) {
       int len = Math.max(0, line.length() - 6);
       line = line.substring(0, len) + "...";
      }
      graphics.drawText(line, LEFT_OFFSET, lineTopOffset);
      break;
     }
    }
   }
  }
 }

 private Vector splitLabelToLines() {
  int fitWidth = mWidth - 2 * LEFT_OFFSET;
  String label = getLabel();
  int lbLen = label.length();
  Vector lines = new Vector();
  int begin = 0;
  // while there are more chars in label
  while (begin < lbLen - 1) {
   // new line
   String lnText = "";
   // line width in pixels
   int lnWidth = 0;
   // line width in chars
   int lnLen = 0;
   // while line fit button width or label chars ends
   while ((lnWidth < fitWidth) && (begin + lnLen < lbLen)) {
    lnLen++;
    lnText = label.substring(begin, begin + lnLen);
    lnWidth = getFont().getAdvance(lnText);
   }
   if (begin + lnLen < lbLen)
    lnLen--;
   begin += lnLen;
   lnText = lnText.substring(0, lnLen);
   lines.addElement(lnText);
  }
  return lines;
 }
}

使用示例:

class Scr extends MainScreen {
 CustomButton button1;
 CustomButton button2;
 CustomButton button3;

 public Scr() {
  add(button1 = new CustomButton(20, 60,
    "first buttton it's with a large text"));
  add(button2 = new CustomButton(40, 120,
    "second buttton it's with a large text"));
  add(button3 = new CustomButton(60, 200,
    "third buttton it's with a large text"));
 }
}