网格布局按钮大小调整问题

时间:2016-01-28 04:18:40

标签: java swing grid-layout codenameone

网格布局中的

按钮img大小不会改变我做的任何事情。以下是我的代码。如果我缩放占位符它也没有效果。当我将填充和边距设置为零时,填充和边距占用的空间也会停留。我怎样才能使它紧凑并摆脱图像之间的差距..

        GridLayout gr = new GridLayout(1,2);
        gr.setAutoFit(true);
        Container grid = new Container(gr);
        grid.setUIID("containerGridImgGallery");
        grid.getAllStyles().setPadding(0, 0, 0, 0);
        grid.getAllStyles().setMargin(0,0,0,0);
        f.addComponent(BorderLayout.CENTER,grid);

        placeholder = (EncodedImage) r.getImage("switch_on.png");
        //if I change the scaled value of placeholder, the img size doesnt change in the grid  eg scaledEncoded(screenWidth / 3, screenWidth / 3) doesnt change the img size
        placeholder = placeholder.scaledEncoded((screenWidth / 2)-10, screenWidth / 3);

        EncodedImage a = URLImage.createToStorage(placeholder , galleryPhotoTitle + offset + imageId, galleryPhotoUrl, URLImage.RESIZE_SCALE_TO_FILL);
        final Button btn = new Button(a);
        btn.setUIID("galleryGridButton");
        btn.getAllStyles().setPadding(0, 0, 0, 0);
        btn.getAllStyles().setMargin(0,0,0,0);

我需要网格布局,因为我可以在gridLayout中设置为gridLayout.autofit(),这就是我想根据屏幕自动调整图像的方式。我如何将网格单元格与其中的按钮相匹配?我可以设置网格的大小吗?

更新

//如何在按钮中使用scaleImageLabel?我得到它的工作不知何故但不正确我猜

EncodedImage a = URLImage.createToStorage(largePlaceholder, galleryPhotoTitle + offset + imageId, galleryPhotoUrl, URLImage.RESIZE_SCALE_TO_FILL);
final Button btn = new Button(a);
Image abc = btn.getIcon();
ScaleImageLabel scaledPlaceholder = new ScaleImageLabel(abc);
Container imageHolder = new Container();
imageHolder.addComponent(btn);
btn.setPreferredSize(new Dimension(1,1));
imageHolder.addComponent(scaledPlaceholder);
imageHolder.setLeadComponent(btn);
grid.addComponent(i, imageHolder);

我有几个问题: 1)为什么我不能在上面设置scaledPlaceholder的uiid(ScaledImageLabel) 它需要一些填充和我要删除的边距。如果我设置图像 不要出现。

2)setBackgroundType()如何工作?它不需要setBackgroundType(Style.BACKGROUND_IMAGE_SCALED_FIT)。

3)最重要的是,我如何在保持纵横比的网格中拟合图像? 如果我喜欢像 - abc = abc.scaled(230,336); 它适合网格,但不保持图像的纵横比。

更新1:

EncodedImage a = URLImage.createToStorage(largePlaceholder, galleryPhotoTitle + offset + imageId, galleryPhotoUrl, URLImage.RESIZE_SCALE_TO_FILL);
ScaleImageButton scaleImageButton = new ScaleImageButton(a);
scaleImageButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
            //......
            }
        });
        grid.addComponent(i, btn);

异常:

java.lang.NullPointerException
    at userclasses.StateMachine$ScaleImageButton.calcPreferredSize(StateMachine.java:500)
    at com.codename1.ui.Component.preferredSizeImpl(Component.java:1930)
    at com.codename1.ui.Component.preferredSize(Component.java:1965)
    at com.codename1.ui.Component.getPreferredSize(Component.java:752)
    at com.codename1.ui.Component.getPreferredW(Component.java:832)
    at com.codename1.ui.layouts.GridLayout.getPreferredSize(GridLayout.java:153)
    at com.codename1.ui.Container.calcPreferredSize(Container.java:1793)
    at com.codename1.ui.Component.preferredSizeImpl(Component.java:1930)
    at com.codename1.ui.Component.preferredSize(Component.java:1965)
    at com.codename1.ui.Component.getPreferredSize(Component.java:752)
    at com.codename1.ui.Component.getPreferredW(Component.java:832)
    at com.codename1.ui.layouts.GridLayout.getPreferredSize(GridLayout.java:153)
    at com.codename1.ui.Container.calcPreferredSize(Container.java:1793)
    at com.codename1.ui.Component.calcScrollSize(Component.java:782)
    at com.codename1.ui.Component.getScrollDimension(Component.java:769)
    at com.codename1.ui.Container.isScrollableY(Container.java:1873)
    at com.codename1.ui.Component.isScrollable(Component.java:1686)
    at com.codename1.ui.Form.isScrollable(Form.java:3058)
    at com.codename1.ui.Component.checkAnimation(Component.java:3802)
    at com.codename1.ui.Component.initComponentImpl(Component.java:4209)
    at com.codename1.ui.Container.initComponentImpl(Container.java:843)
    at com.codename1.ui.Form.initComponentImpl(Form.java:1608)
    at com.codename1.ui.Display.setCurrent(Display.java:1332)
    at com.codename1.ui.Form.show(Form.java:1588)
    at com.codename1.ui.Form.show(Form.java:1566)
    at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2515)
    at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2561)
    at userclasses.StateMachine.showGalleryImage(StateMachine.java:159)
    at userclasses.StateMachine$1.lambda$readResponse$0(StateMachine.java:136)
    at userclasses.StateMachine$1$$Lambda$17/1247957021.actionPerformed(Unknown Source)
    at com.codename1.ui.util.EventDispatcher.fireActionEvent(EventDispatcher.java:345)
    at com.codename1.ui.Button.fireActionEvent(Button.java:397)
    at com.codename1.ui.Button.released(Button.java:428)
    at com.codename1.ui.Button.pointerReleased(Button.java:516)
    at com.codename1.ui.Form.pointerReleased(Form.java:2560)
    at com.codename1.ui.Form.pointerReleased(Form.java:2496)
    at com.codename1.ui.Component.pointerReleased(Component.java:3108)
    at com.codename1.ui.Display.handleEvent(Display.java:2017)
    at com.codename1.ui.Display.edtLoopImpl(Display.java:1065)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:994)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)

错误发生在这里

 @Override
        protected Dimension calcPreferredSize() {
            Image i = getIcon();
            Style s = getStyle();
         //error occured here
            return new Dimension(i.getWidth() + s.getPaddingLeft(false) + s.getPaddingRight(false), i.getHeight()
                    + s.getPaddingTop() + s.getPaddingBottom());
        }

如果我使用label而不是btn,还可以使用它,但是在按钮的情况下同样不起作用,为什么会这样?

更新2:添加getIconFromState方法后,显示空白表单 - 相同的旧问题,如果删除了null检查程序,则出现以下错误

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.codename1.impl.javase.Executor$1$1.run(Executor.java:100)
    at com.codename1.ui.Display.processSerialCalls(Display.java:1149)
    at com.codename1.ui.Display.mainEDTLoop(Display.java:966)
    at com.codename1.ui.RunnableWrapper.run(RunnableWrapper.java:120)
    at com.codename1.impl.CodenameOneThread.run(CodenameOneThread.java:176)
Caused by: java.lang.NullPointerException
    at userclasses.StateMachine$ScaleImageButton.calcPreferredSize(StateMachine.java:303)
    at com.codename1.ui.Component.preferredSizeImpl(Component.java:1930)
    at com.codename1.ui.Component.preferredSize(Component.java:1965)
    at com.codename1.ui.Component.getPreferredSize(Component.java:752)
    at com.codename1.ui.Component.getPreferredW(Component.java:832)
    at com.codename1.ui.layouts.GridLayout.getPreferredSize(GridLayout.java:153)
    at com.codename1.ui.Container.calcPreferredSize(Container.java:1793)
    at com.codename1.ui.Component.preferredSizeImpl(Component.java:1930)
    at com.codename1.ui.Component.preferredSize(Component.java:1965)
    at com.codename1.ui.Component.getPreferredSize(Component.java:752)
    at com.codename1.ui.Component.getPreferredH(Component.java:842)
    at com.codename1.ui.layouts.BoxLayout.layoutContainer(BoxLayout.java:91)
    at com.codename1.ui.Container.doLayout(Container.java:1366)
    at com.codename1.ui.Container.layoutContainer(Container.java:1358)
    at com.codename1.ui.Container.doLayout(Container.java:1371)
    at com.codename1.ui.Container.layoutContainer(Container.java:1358)
    at com.codename1.ui.Container.revalidate(Container.java:1006)
    at com.codename1.ui.Form.setFocused(Form.java:1922)
    at com.codename1.ui.Form.initFocused(Form.java:1553)
    at com.codename1.ui.Form.show(Form.java:1584)
    at com.codename1.ui.Form.show(Form.java:1566)
    at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2515)
    at com.codename1.ui.util.UIBuilder.showForm(UIBuilder.java:2561)
    at generated.StateMachineBase.startApp(StateMachineBase.java:56)
    at generated.StateMachineBase.<init>(StateMachineBase.java:31)
    at generated.StateMachineBase.<init>(StateMachineBase.java:98)
    at userclasses.StateMachine.<init>(StateMachine.java:51)
    at com.mycompany.myapp.MyApplication.start(MyApplication.java:23)
    ... 9 more

更新3: 截图现在看起来像这样: 肖像:

enter image description here

景观: enter image description here

然而,如果我从纵向模式进入横向模式并再次进入纵向模式,图像会按预期调整,但我仍然可以在那里看到薄的重复图像部分。

从纵向到横向再到纵向的屏幕截图:

enter image description here

1 个答案:

答案 0 :(得分:0)

下载图像并匹配占位符大小后,它们将存储/缓存在系统中。由于Label(或导出它的Button)不会缩放图标图像,因此不会更改。

下载后更改占位符大小也不起作用。

对于3.3,我们引入了一个新的ScaleImageLabel类,它应该允许标签在这种情况下扩展以占用额外的空间。请注意,由于缩放,可能会应用工件。

由于您需要$ android list sdk -a | grep "Android Support" 146- Android Support Repository, revision 25 147- Android Support Library, revision 23.1.1 而不是标签,请尝试以下操作:

Button