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开发者_JAVA技巧 the text and sometimes not even showing single line.If u have any idea to solve this prob pls share with me.
My implementation of button control will:
- have fixed size, font and text offset
- if label cannot fit single line, show label in several lines of maximum width
- if no more lines can fit, show ellipsis at the end of the line
alt text http://img297.imageshack.us/img297/8360/multilinebuttons.jpg
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;
}
}
Example of use:
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"));
}
}
精彩评论