使用scaledImageButton类来缩放网格单元格中的按钮,但不知何故它不能正常工作。当我将scaleImageButton类的背景行为类型保持为scale_Image_To_Fit时,网格中存在间隙,如果我将其设置为scale_Image_To_FILL,则在上面的网格中创建的间隙将被其自己的缩放图像替换。因此在单个网格单元格中看到两个重复的图像。代码和屏幕截图如下:
我的代码:
GridLayout gr = new GridLayout(1, 2);
gr.setAutoFit(true);
Container gridContainer = new Container(gr);
gridContainer.setUIID("containerGridImgGallery");
gridContainer.getAllStyles().setPadding(0, 0, 0, 0);
gridContainer.getAllStyles().setMargin(0, 0, 0, 0);
gridContainer.getAllStyles().setBorder(Border.createEmpty());
gridContainer.setScrollableY(true);
EncodedImage a = URLImage.createToStorage(placeholder, galleryPhotoTitle + offset + imageId + "bibek", galleryPhotoUrl, URLImage.RESIZE_SCALE_TO_FILL);
ScaleImageButton scaleImageButton = new ScaleImageButton();
scaleImageButton.setIcon(a);
gridContainer.addComponent(scaleImageButton)
f.addComponent(gridContainer);
f.revalidate();
ScaleImageButton类:
public class ScaleImageButton extends Button {
/**
* Default constructor
*/
public ScaleImageButton() {
setUIID("Container");
setShowEvenIfBlank(true);
setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
}
/**
* Create a version with the given image
*
* @param i image
*/
public ScaleImageButton(Image i) {
setUIID("Container");
setShowEvenIfBlank(true);
setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT);
setIcon(i);
}
/**
* Sets the behavior of the background to one of
* Style.BACKGROUND_IMAGE_SCALED_FIT,
* Style.BACKGROUND_IMAGE_SCALED_FILL, Style.BACKGROUND_IMAGE_SCALE
*
* @param behavior the background behavior
*/
public void setBackgroundType(byte behavior) {
getUnselectedStyle().setBackgroundType(behavior);
getSelectedStyle().setBackgroundType(behavior);
getPressedStyle().setBackgroundType(behavior);
}
public byte getBackgroundType() {
return getUnselectedStyle().getBackgroundType();
}
@Override
protected Dimension calcPreferredSize() {
Image i = getIcon();
if (i == null) {
return new Dimension();
}
Style s = getStyle();
return new Dimension(i.getWidth() + s.getPaddingLeft(false) + s.getPaddingRight(false), i.getHeight()
+ s.getPaddingTop() + s.getPaddingBottom());
}
public void setIcon(Image i) {
getUnselectedStyle().setBgImage(i);
getSelectedStyle().setBgImage(i);
getPressedStyle().setBgImage(i);
}
public Image getIcon() {
return getUnselectedStyle().getBgImage();
}
@Override
public void setText(String text) {
}
@Override
public Image getIconFromState() {
return getIcon();
}
@Override
public void setUIID(String id) {
Image icon = getIcon();
super.setUIID(id);
setIcon(icon);
}
}
Style.BACKGROUND_IMAGE_SCALED_FIT
的背景行为类型Style.BACKGROUND_IMAGE_SCALED_FILL的背景行为类型