2013-05-25 42 views

回答

1

如果有访问黑莓的知识库(?即一些国家)的一个问题,这里要说的是页面(由RIM的@MSohm发布)内容:

一个RichTextField不确实nativly支持标,上标或 多种颜色。支持多种字体,字体大小和字体格式 (例如,粗体,斜体,带下划线)。以下 链接解释了这一点。

如何 - 格式文本在RichTextField文章编号:DB-00124
http://supportforums.blackberry.com/t5/Java-Development/Format-text-in-a-RichTextField/ta-p/445038

如何 - 更改字段物品编码的文本颜色:DB-00114
http://supportforums.blackberry.com/t5/Java-Development/Change-the-text-color-of-a-field/ta-p/442951


如果你仍然想这样做,你可以尝试子类RichTextFieldLabelField,并覆盖paint()方法。在那里,您可以更改字体大小并移动文本的y坐标。这取决于您希望如何制作解决方案。也许你可以发布更多关于你的问题的信息?

但是,作为一个非常简单的,硬编码例如,下面的代码将创建一个LabelField打印出: “CO

private class SubscriptLabelField extends LabelField { 

     private int _subscriptTop = 0; 
     private int _subscriptFontSize = 0; 

     public SubscriptLabelField(Object text, long style) { 
     super(text, style); 
     setFont(getFont()); 
     } 

     public void setFont(Font newFont) { 
     super.setFont(newFont);   

     // we use a subscript that's located at half the normal font's height, 
     // and is 2/3 as tall as the normal font 
     int h = newFont.getHeight(); 
     _subscriptTop = h/2; 
     _subscriptFontSize = 2 * h/3; 
     super.invalidate(); 
     } 

     protected void layout(int width, int height) { 
     super.layout(width, height); 

     // add more space at the bottom for the subscript 
     int w = getExtent().width; 
     int h = getExtent().height; 
     int extraHeight = _subscriptFontSize - (getFont().getHeight() - _subscriptTop); 
     setExtent(w, h + extraHeight); 
     } 

     public void paint(Graphics g) { 
     // here we hardcode this method to simply draw the last char 
     // as a "subscript" 
     String text = getText(); 
     String normalText = text.substring(0, text.length() - 1); 
     g.drawText(normalText, 0, 0); 

     // how much space will the normal text take up, horizontally? 
     int advance = g.getFont().getAdvance(normalText); 

     // make the subscript a smaller font 
     Font oldFont = g.getFont(); 
     Font subscript = getFont().derive(Font.PLAIN, _subscriptFontSize); 
     g.setFont(subscript); 
     String subscriptText = text.substring(text.length() - 1); 
     g.drawText(subscriptText, advance, _subscriptTop); 

     // reset changes to graphics object just to be safe 
     g.setFont(oldFont); 
     } 
    } 

,然后用它是这样的:

public SubscriptScreen() { 
    super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR); 

    SubscriptLabelField textField = new SubscriptLabelField("C02", LabelField.NON_FOCUSABLE); 

    // TODO: this line is just to show the adjusted boundaries of the field -> remove! 
    textField.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTGRAY)); 

    add(textField); 
} 

这给:

enter image description here

+0

感谢哥们你让我的日子 –

+0

不客气:) – Nate