我正在实现自定义的标签字段(LabelField)在我的应用程序中。当使用小字体时它可以正常工作,但是当我增大字体大小时会出现显示不正常的情况。您可以在此图片中看到。
从图片中可以看到,它只显示文本的一半。我该如何解决这个问题。
这里是我自定义LabelField的代码。请告诉我我做错了什么。
public class CustomLabelField extends Field
{
private String label;
private int fontSize;
private int foregroundColor;
private int backgroundColor;
public CustomLabelField(String label, int fontSize, int foregroundColor,
int backgroundColor,long style)
{
super(style);
this.label = label;
this.foregroundColor = foregroundColor;
this.backgroundColor = backgroundColor;
this.fontSize = fontSize;
}
protected void layout(int width, int height) {
setExtent(width, getFont().getHeight());
}
protected void paint(Graphics graphics) {
graphics.setBackgroundColor(backgroundColor);
graphics.clear();
graphics.setFont(setMyFont());
graphics.setColor(foregroundColor);
graphics.drawText(label, 0, 0, (int)(getStyle()& DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),getWidth() - 6);
}
// Get font for the label field
public Font setMyFont()
{
try {
FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");
Font appFont = alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt);
return appFont;
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}