简体   繁体   English

一种布局将另一种布局推离屏幕

[英]One layout pushes the other one off the screen

Currently i'm working one a small android project. 目前,我正在做一个小型android项目。 I ran on a problem with layouts because I must create a user interface in Java. 我遇到了布局问题,因为必须使用Java创建用户界面。 I have 2 layouts first is the LinearLayout with horizontal orientation and the other is a TableLayout. 我有2个布局,第一个是水平方向的LinearLayout,另一个是TableLayout。 I set the width MATCH PARENT and height WRAP CONTENT for both of them but the first one pushes the other one off the screen and I want the first layout to push the other one under him. 我为它们都设置了宽度MATCH PARENT和高度WRAP CONTENT,但是第一个将另一个推离屏幕,我希望第一个布局将另一个推向他下方。

The code looks like this: 代码如下:

    LinearLayout mainLayout = new LinearLayout(this);

    LinearLayout hLayout = new LinearLayout(this);
    hLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
    LayoutParams.WRAP_CONTENT); 
    hLayout.setLayoutParams(params);

    mainLayout.addView(hLayout);

    TableLayout table = new TableLayout(this);

    params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

    table.setLayoutParams(params);

    mainLayout.addView(table);
    setContentView(mainLayout);

You don't seem to add hLayout to anything so it isn't being pushed off the screen but never added to the content view. 您似乎没有将hLayout添加到任何内容,因此它不会被推离屏幕,但从未添加到内容视图中。 Try something like 尝试类似

mainLayout.addView(table);
mainLayout.addView(hLayout);
setContentView(mainLayout);

Also, since the width is match_parent and LinearLayout has a default orientation of horizontal, you will either need to change the orientation of the mainLayout to vertical or change the width to wrap_content . 此外,由于宽度match_parentLinearLayout有一个默认orientation的水平,你要么需要改变orientation的的mainLayout垂直或改变widthwrap_content

And is there a reason you are doing it programmatically? 您是否有理由以编程方式进行此操作? Creating layout s in xml is typically much easier. 通常,在xml中创建layout更加容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM