Android LinearLayout仅允许添加一个视图

Android LinearLayout仅允许添加一个视图,android,android-layout,Android,Android Layout,我知道我一定遗漏了什么,因为每当我向这个布局添加视图时,我只会显示其中一个视图。线性布局的方向必须是水平的。将其更改为垂直线性布局的方向必须是水平的。将其更改为垂直享受好友 layout = new LinearLayout(this); addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); Button

我知道我一定遗漏了什么,因为每当我向这个布局添加视图时,我只会显示其中一个视图。

线性布局的方向必须是水平的。将其更改为垂直

线性布局的方向必须是水平的。将其更改为垂直

享受好友

  layout = new LinearLayout(this);
            addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


            Button btn = new Button(this);
            btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn.setText("button");

            layout.addView(btn);

            Button btn1 = new Button(this);
            btn1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn1.setText("button");

            layout.addView(btn1);
 layout = new LinearLayout(this);
layout .setOrientation(LinearLayout.VERTICAL); // orientation vertical try  this

            addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


            Button btn = new Button(this);
            btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn.setText("button");

            layout.addView(btn);

            Button btn1 = new Button(this);
            btn1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn1.setText("button");

            layout.addView(btn1);
重新命名此addContentViewlayout,新建LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容

您尚未设置方向,默认值为水平,并且已指定宽度布局参数。填充父对象

 layout = new LinearLayout(this);
layout .setOrientation(LinearLayout.VERTICAL); // orientation vertical try  this

            addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));


            Button btn = new Button(this);
            btn.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn.setText("button");

            layout.addView(btn);

            Button btn1 = new Button(this);
            btn1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
           btn1.setText("button");

            layout.addView(btn1);
重新命名此addContentViewlayout,新建LayoutParams.FILL\u父级,LayoutParams.WRAP\u内容


您尚未设置方向,默认值为水平,并且您已指定宽度布局参数。填充父对象

y您是否尝试通过xml添加按钮?是否尝试通过xml添加按钮?