【问题标题】:display an Android LinearLayout in a Codename One application在 Codename One 应用程序中显示 Android LinearLayout
【发布时间】:2021-08-12 20:17:37
【问题描述】:

我可以用原生android代码创建一个LinearLayout(例如)并显示在CN1界面上吗?

我有一个实现原生 android 代码的方法来创建这个视图。例如,如果该方法返回一个 EditText 或 Button,则一切正常。但如果它返回一个 LinearLayout,什么都不会发生,我也没有错误消息。

调用本机代码的方法的签名:

public interface NativeCall extends NativeInterface {
    
    public PeerComponent getMainView();
    
}

运行良好的原生实现代码:

    public class NativeCallImpl {

        public android.view.View getMainView() {
    
            Button b0 = new Button(((Context) MyApplication.getContext()));                        
            return b0;
        }
    
        public boolean isSupported() {
            return true;
        }

}

本地代码实现不起作用:

public class NativeCallImpl {
       
    public android.view.View getMainView() {

        Button b0 = new Button(((Context) MyApplication.getContext()));
        Button b1 = new Button(((Context) MyApplication.getContext()));

        b0.setText("b0");
        b1.setText("b1");

        LinearLayout linearLayout = new LinearLayout(((Context) MyApplication.getContext()));
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        linearLayout.addView(b0);
        linearLayout.addView(b1);

        return linearLayout;
    }

    public boolean isSupported() {
        return true;
    }

}

【问题讨论】:

    标签: android android-layout codenameone


    【解决方案1】:

    是的。线性布局应该可以工作。尝试将 UI 添加到具有边框布局的表单的中心。这将抵消滚动/首选大小计算等的影响。

    Android 布局计算有点过于复杂,因此它们的尺寸测量代码在第一次尝试布局时效果不佳。对于一个按钮来说它会很好用,但对于一个布局来说......不是那么多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多