如何在Codenameone中实现Floating Action Button?

How to achieve Floating Action Button in Codenameone?

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();

这是 link 类似于我想要实现的。 https://github.com/Clans/FloatingActionButton 请帮忙! 谢谢!

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

    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% 正确,但现在有一个内置的浮动按钮组件:https://www.codenameone.com/blog/floating-button.html