问题
I have written BlackBerry code to add an image to a ButtonField. I want the whole button to be occupied by the image, but the image is not displayed completely on the ButtonField. There is a lot of margin on the top, left and right of the button. I tried to use cellpadding but it didn't work.
How can I reduce the width and height of the ButtonField so it matches the original image size of 41 x 41?
回答1:
Instead of adding an image to a button extend the image class and turn that into a button by overriding isFocusable(), navigationClick(), trackwheelClick(), and keyChar(). Here's the code:
public class ImageButtonField extends BitmapField
{
public ImageButtonField(Bitmap image) {
super(image);
}
public boolean isFocusable() {
return true;
}
protected boolean navigationClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick(int status, int time) {
fieldChangeNotify(0);
return true;
}
protected boolean keyChar(char character, int status, int time) {
if(Characters.ENTER == character || Characters.SPACE == character) {
fieldChangeNotify(0);
return true;
}
return super.keyChar(character, status, time);
}
}
来源:https://stackoverflow.com/questions/4509519/blackberry-how-to-occupy-a-complete-button-with-image