2011-12-30 63 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來更改此行爲。