本文介绍了LinearLayout仅允许添加一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我知道我一定会缺少一些东西,因为每当我向该布局添加视图时,我只会显示其中一个视图.

I know I must be missing something because whenever I add views to this layout I only have one of them show up.

推荐答案

享受好友

layout = new LinearLayout(this);
layout.setLayoutParams(new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
layout.setOrientation(LinearLayout.VERTICAL);
setContentView(layout);
layout.setOrientation(LinearLayout.VERTICAL);

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

您尚未设置Oreientation,默认为Horizo​​ntal,并且已指定宽度LayoutParams.FILL_PARENT

You have not set Oreientation , Default is Horizontal, and you have given width LayoutParams.FILL_PARENT

这篇关于LinearLayout仅允许添加一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 09:06