如何在Codenameone中实现浮动操作按钮?

如何在Codenameone中实现浮动操作按钮?,codenameone,floating-action-button,Codenameone,Floating Action Button,android中的浮动操作按钮是一个不错的选择。我想在我的codenameone应用程序中使用它。我尝试过使用LayeredLayout,使用两种布局。我无法完美地完成它。按钮会随着滚动条不断移动。如何在不影响背景层滚动的情况下将按钮固定在屏幕右下角 下面是我如何尝试的 Form myForm = new Form(); myForm.setLayout(new LayeredLayout()); myForm.setTitle("Floating Action Button"); SpanL

android中的浮动操作按钮是一个不错的选择。我想在我的codenameone应用程序中使用它。我尝试过使用LayeredLayout,使用两种布局。我无法完美地完成它。按钮会随着滚动条不断移动。如何在不影响背景层滚动的情况下将按钮固定在屏幕右下角

下面是我如何尝试的

Form myForm = new Form();
myForm.setLayout(new LayeredLayout());
myForm.setTitle("Floating Action Button");

SpanLabel lbl = new SpanLabel("some long text");

Container conBottom = new Container();
conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
conBottom.addComponent(lbl);

FlowLayout flow = new FlowLayout(Component.RIGHT);
flow.setValign(Component.BOTTOM);
Container conUpper = new Container(flow);
conUpper.addComponent(new Button("+"));
conUpper.setScrollable(false);

myForm.addComponent(conBottom);
myForm.addComponent(conUpper);

myForm.show();
下面是与我想要实现的类似的链接。 请帮忙!
谢谢

表单的内容窗格正在执行滚动,您需要底部容器来处理滚动。 试试这个:

    Form myForm = new Form();
    myForm.setLayout(new LayeredLayout());
    myForm.setTitle("Floating Action Button");

    SpanLabel lbl = new SpanLabel("some long text ");

    Container conBottom = new Container();
    conBottom.setScrollableY(true);
    conBottom.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
    conBottom.addComponent(lbl);

    FlowLayout flow = new FlowLayout(Component.RIGHT);
    flow.setValign(Component.BOTTOM);
    Container conUpper = new Container(flow);
    conUpper.addComponent(new Button("+"));
    conUpper.setScrollable(false);

    myForm.addComponent(conBottom);
    myForm.addComponent(conUpper);
    myForm.setScrollable(false);
    myForm.show();

实现这一点的另一种方法是将按钮放置在表单LayeredPane上。这允许您自定义表单布局以处理任何需要的内容。这样,您就不必将表单设置为LayeredLayout。下面是一段代码,您可以使用它来实现:

    FlowLayout fl = new FlowLayout(Component.RIGHT);
    fl.setValign(Component.BOTTOM);
    Container cont = new Container(fl);
    Button btn = new Button("+");
    //OR
    //Button btn = new Button(getImageFromTheme("plus_icon.png"));
    btn.addActionListener(null);
    btn.setUIID("ButtonFloat");
    cont.addComponent(btn);
    myForm.getLayeredPane().addComponent(cont);
    myForm.getLayeredPane().setLayout(new LayeredLayout());

    btn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent evt) {
           //Handle your btn click here
        }
    });

虽然其他答案仍然100%正确,但现在有一个内置浮动按钮组件: