为RichTextField和TextField设置背景和字体颜色

3

我们如何在RichTextField中设置背景和字体颜色?我试图覆盖paint()方法,除了这里描述的内容之外,但是当我向下滚动时,背景会被擦除或重置为白色背景。

2个回答

4
在 RIM 4.6 及更高版本中,您可以使用 Background: 来设置背景:
class ExRichTextField extends RichTextField {

    int mTextColor;

    public ExRichTextField(String text, int bgColor, int textColor) {
        super(text);
        mTextColor = textColor;
        Background background = BackgroundFactory
                .createSolidBackground(bgColor);
        setBackground(background);
    }

    protected void paint(Graphics graphics) {
        graphics.setColor(mTextColor);
        super.paint(graphics);
    }
}

对于RIM 4.5及以下版本,使用paint事件来自己绘制背景:

class ExRichTextField extends RichTextField {

    int mTextColor;
    int mBgColor;

    public ExRichTextField(String text, int bgColor, int textColor) {
        super(text);
        mTextColor = textColor;
        mBgColor = bgColor;
    }

    protected void paint(Graphics graphics) {
        graphics.clear();
        graphics.setColor(mBgColor);
        graphics.fillRect(0, 0, getWidth(), getHeight());
        graphics.setColor(mTextColor);
        super.paint(graphics);
    }
}

谢谢,运行得非常顺畅!但是,当我将它添加到VerticalFieldManager中时,我看不到滚动条,我已经将Manager的样式设置为VERTICAL_SCROLL} VERTICAL_SCROLLBAR。有什么原因它没有出现吗? - Prabhu R
无法解决这个问题...当管理器的首选大小设置时,似乎滚动条没有出现。 - Maksym Gontar
我遇到的另一个问题是,当我在BitmapField之后将ExRichTextField添加到VerticalFieldManager中时,整个屏幕会滚动而不是仅滚动ExRichTextField,这是期望的行为。将两个管理器添加到类中是否可以解决这个问题? - Prabhu R
嗨Ram!似乎没有解决标准滚动条的方法,所以我只能自定义绘制它。请参见更新http://stackoverflow.com/questions/1426081/creating-custom-layouts-in-blackberry/1428106#1428106 - Maksym Gontar

0
RichTextField mes_=new RichTextField("texto de ejemplo",Field.NON_FOCUSABLE){
    protected void paint(Graphics g){ 
        g.setColor(0x00e52f64);
        super.paint(g);
    }
};
mes_.setBackground(BackgroundFactory.createSolidBackground(0xFFFADDDA));

声明中嵌入的方法是用于更改字体颜色的。创建后调用的方法是用于将背景更改为纯色。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接