2011-12-30 66 views
0

我正在使用以下代码将图像添加到按钮。但是,我从窗口视图中获得了一些按钮!我应该添加什么来使它们适合视图窗口?!黑莓BitmapButtonField中心对齐+在窗口视图上拟合

加上:我希望他们在中心,垂直和水平。假设我有15个按钮,每行需要3个按钮。并在屏幕的中心?

在此先感谢

public class BitmapButtonField extends ButtonField { 
    private Bitmap bitmap; 
    private Bitmap bitmapHighlight; 
    private boolean highlighted = false; 

    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight) { 
     this(bitmap, bitmapHighlight, ButtonField.CONSUME_CLICK|ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER); 
    } 

    public BitmapButtonField(Bitmap bitmap, Bitmap bitmapHighlight, long style) { 
     super(style); 
     this.bitmap = bitmap; 
     this.bitmapHighlight = bitmapHighlight; 
    } 

    protected void layout(int width, int height) { 
      setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    public int getPreferredWidth() { 
      return bitmap.getWidth(); 
    } 

    public int getPreferredHeight() { 
      return bitmap.getHeight(); 
    } 

    protected void paint(Graphics graphics) { 
      super.paint(graphics); 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 
      Bitmap b = bitmap; 
      if (highlighted) 
       b = bitmapHighlight; 
      graphics.drawBitmap(0, 0, width, height, b, 0, 0); 
    } 
} 
+0

如果你想要你的ButtonField居中,垂直和水平,你必须实现一个自定义FieldManager,它将放置ButtonField实例。您的BitmapButtonField类似乎没问题,只有位图宽度超过父级管理员提供的最大允许宽度时才会导致一些问题。 – Rupak 2012-01-01 14:46:30

回答

0

没有什么不对您的领域。很可能你使用的是HorizontalFieldManager,它具有无限宽度,并且将字段放在屏幕边界之外。您可以使用FlowFieldManager来更改此行为。