2012-04-12 57 views
3

我正在自己的应用中实现自定义标签栏。它使用小字体工作正常,当我增加字体大小时,它将无法正常显示。在这里你可以在这张图片中看到它。黑莓中的自定义标签栏文字不能正确显示

enter image description here

您可以在图片中看到它是只显示文本的一半。我该如何克服这一点。

这里我给我的代码自定义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; 
    } 

} 

回答

3

在你layout方法你所在领域的高度设置为当前默认的字体高度。这意味着当您使用较大的字体进行绘制时,文本会被裁剪。为了解决这个改变你的布局方法:

protected void layout(int width, int height) { 

    int fieldHeight = Ui.convertSize(fontSize, Ui.UNITS_pt, Ui.UNITS_px); 
    setExtent(width, fieldHeight); 
} 

这是你想要的字体大小转换为像素,并使用该设置您的场高度。

3

如何通过扩展LabelField来创建您的CustomLabelField?然后,如果在构造函数中设置了适当的样式位,则布局,绘制文本的复杂性(对齐,包装,样式考虑等)和其他任务将由LabelField本身完成。

您只需调用setFont(Font)即可应用该Field上的任何字体。该领域本身将调整其大小和绘图。检查以下片段。


CustomLabelField实现:

class CustomLabelField extends LabelField { 
    private int foregroundColor; 
    private int backgroundColor; 

    public CustomLabelField(String label, int foregroundColor, 
      int backgroundColor, long style) { 
     super(label, style); 
     this.foregroundColor = foregroundColor; 
     this.backgroundColor = backgroundColor; 
    } 

    protected void paint(Graphics graphics) { 
     graphics.setBackgroundColor(backgroundColor); 
     graphics.clear(); 
     graphics.setColor(foregroundColor); 
     super.paint(graphics); 
    } 
} 


实施例:

class MyScreen extends MainScreen { 

    public Font getMyFont(int fontSize) { 
     try { 
      FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif"); 
      return alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt); 
     } catch (Exception e) { 
     } 
     return null; 
    } 

    public MyScreen() { 
     CustomLabelField clf = new CustomLabelField(
        "Main", 
        Color.WHITE, 
        Color.BLACK, 
        LabelField.ELLIPSIS); 

     // Font setup 
     clf.setFont(getMyFont(20)); 

     add(clf); 
    } 
}