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);
}
}
如果你想要你的ButtonField居中,垂直和水平,你必須實現一個自定義FieldManager,它將放置ButtonField實例。您的BitmapButtonField類似乎沒問題,只有位圖寬度超過父級管理員提供的最大允許寬度時纔會導致一些問題。 – Rupak 2012-01-01 14:46:30