如何在黑莓中使用自定义标签字段显示多行文本?当我使用下面的代码时,我可以用所需的字体大小显示标签,但是问题是,当范围在宽度上受到限制时,文本会被截断,并且标签部分的其余部分也不会显示在下一行上。
感谢您在这里提供的任何帮助。
以下是我的自定义标签字段的代码。
public class GrayBgLabelField extends LabelField {
private int width = Display.getWidth();
private int height = 40;
private String label;
private Font font;
public GrayBgLabelField(){
super();
}
public GrayBgLabelField(String label, int fieldWidth){
super(label, 0 );
this.label = label;
width = fieldWidth;
}
public GrayBgLabelField(String label, int fieldWidth , long style){
super( label, style );
this.label = label;
width = fieldWidth;
}
public GrayBgLabelField(String label, int fieldWidth , int fieldHeight){
this(label,fieldWidth);
this.label = label;
height = fieldHeight;
}
public GrayBgLabelField(String label, int fieldWidth , int fieldHeight, long style){
super( label, style );
this.label = label;
width = fieldWidth;
height = fieldHeight;
}
protected void layout( int maxWidth, int maxHeight)
{
super.layout( width, height);
setExtent( width, height);
}
protected void paint(Graphics g) {
if(font !=null){
g.setFont(font);
}
if (label.length() != 0) {
g.drawText(label, width/30, height/4);
}
g.setColor(Color.BLACK);
}
public void setFont(int f){
font = this.getFont().derive(f);
}
public void setFont(Font font){
this.font = font;
}
protected void paintBackground(Graphics g) {
int oldColor = g.getColor();
try {
g.setColor(0xF5F6F8); // Gray-DDDDDD color code.
g.fillRect(0, 0, getWidth(), getHeight());
} finally {
g.setColor(oldColor);
}
}
}
发布于 2012-10-06 05:27:11
重写控制自定义字段宽度和高度的方法。
以下是教程:"How to create a custom field"
当您更改标签字段文本时,通过invalidate()
方法使其无效,以便在屏幕上重新绘制字段内容。
https://stackoverflow.com/questions/12743726
复制相似问题