如何创建CustomLabelField这套房为所有黑莓版本? [英] How to create CustomLabelField which suites for all blackberry version?

查看:107
本文介绍了如何创建CustomLabelField这套房为所有黑莓版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了自己的customlabelfield,使用这种customlabelfield我可以能够修改背景颜色,字体颜色,宽度和高度......当我在4.6版中使用这个标签字段我能得到预期的输出一样,如果文本不适合在单行然后它会自动涉及到二线,但如果我用同样的在4.5版本中的文本不来下一行,而是有时显示文本的一半,有时甚至不显示单line.If u有任何想法解决这个概率请跟我分享一下。

I have created my own customlabelfield, using this customlabelfield i can able to modify the background color,font color,width and height...When i use this label field in the version 4.6 i can get the expected output like,if the text is not suite in single line then it automatically comes to second line but if i use the same in version 4.5 the text is not coming to next line and instead sometimes it showing half of the text and sometimes not even showing single line.If u have any idea to solve this prob pls share with me.

推荐答案

我的实现按钮控制将:


  • 有固定的大小,字体和文本偏移

  • 如果标签无法容纳一行,显示在标签的最大宽度的几行

  • 如果没有更多的线路可以适合,在该行的末尾显示省略号



自定义按钮code:


A custom button code:

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

这篇关于如何创建CustomLabelField这套房为所有黑莓版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆